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; 284b2cd27e8SJed 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); 287b2cd27e8SJed 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); 294b2cd27e8SJed 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 { 1162*9e2a6581SJed Brown PetscErrorCode ierr; 1163*9e2a6581SJed Brown 1164d763cef2SBarry Smith PetscFunctionBegin; 11650700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1166bdad233fSMatthew Knepley ts->problem_type = type; 1167*9e2a6581SJed Brown if (type == TS_LINEAR) { 1168*9e2a6581SJed Brown SNES snes; 1169*9e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1170*9e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 1171*9e2a6581SJed Brown } 1172d763cef2SBarry Smith PetscFunctionReturn(0); 1173d763cef2SBarry Smith } 1174d763cef2SBarry Smith 1175bdad233fSMatthew Knepley #undef __FUNCT__ 1176bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1177bdad233fSMatthew Knepley /*@C 1178bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1179bdad233fSMatthew Knepley 1180bdad233fSMatthew Knepley Not collective 1181bdad233fSMatthew Knepley 1182bdad233fSMatthew Knepley Input Parameter: 1183bdad233fSMatthew Knepley . ts - The TS 1184bdad233fSMatthew Knepley 1185bdad233fSMatthew Knepley Output Parameter: 1186bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1187bdad233fSMatthew Knepley .vb 1188089b2837SJed Brown M U_t = A U 1189089b2837SJed Brown M(t) U_t = A(t) U 1190bdad233fSMatthew Knepley U_t = F(t,U) 1191bdad233fSMatthew Knepley .ve 1192bdad233fSMatthew Knepley 1193bdad233fSMatthew Knepley Level: beginner 1194bdad233fSMatthew Knepley 1195bdad233fSMatthew Knepley .keywords: TS, problem type 1196bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1197bdad233fSMatthew Knepley @*/ 11987087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1199a7cc72afSBarry Smith { 1200bdad233fSMatthew Knepley PetscFunctionBegin; 12010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 12024482741eSBarry Smith PetscValidIntPointer(type,2); 1203bdad233fSMatthew Knepley *type = ts->problem_type; 1204bdad233fSMatthew Knepley PetscFunctionReturn(0); 1205bdad233fSMatthew Knepley } 1206d763cef2SBarry Smith 12074a2ae208SSatish Balay #undef __FUNCT__ 12084a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1209d763cef2SBarry Smith /*@ 1210d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1211d763cef2SBarry Smith of a timestepper. 1212d763cef2SBarry Smith 1213d763cef2SBarry Smith Collective on TS 1214d763cef2SBarry Smith 1215d763cef2SBarry Smith Input Parameter: 1216d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1217d763cef2SBarry Smith 1218d763cef2SBarry Smith Notes: 1219d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1220d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1221d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1222d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1223d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1224d763cef2SBarry Smith 1225d763cef2SBarry Smith Level: advanced 1226d763cef2SBarry Smith 1227d763cef2SBarry Smith .keywords: TS, timestep, setup 1228d763cef2SBarry Smith 1229d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1230d763cef2SBarry Smith @*/ 12317087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1232d763cef2SBarry Smith { 1233dfbe8321SBarry Smith PetscErrorCode ierr; 1234d763cef2SBarry Smith 1235d763cef2SBarry Smith PetscFunctionBegin; 12360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1237277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1238277b19d0SLisandro Dalcin 12397adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 12409596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1241d763cef2SBarry Smith } 1242277b19d0SLisandro Dalcin 1243277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1244277b19d0SLisandro Dalcin 1245277b19d0SLisandro Dalcin if (ts->ops->setup) { 1246000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1247277b19d0SLisandro Dalcin } 1248277b19d0SLisandro Dalcin 1249277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1250277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1251277b19d0SLisandro Dalcin } 1252277b19d0SLisandro Dalcin 1253277b19d0SLisandro Dalcin #undef __FUNCT__ 1254277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1255277b19d0SLisandro Dalcin /*@ 1256277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1257277b19d0SLisandro Dalcin 1258277b19d0SLisandro Dalcin Collective on TS 1259277b19d0SLisandro Dalcin 1260277b19d0SLisandro Dalcin Input Parameter: 1261277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1262277b19d0SLisandro Dalcin 1263277b19d0SLisandro Dalcin Level: beginner 1264277b19d0SLisandro Dalcin 1265277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1266277b19d0SLisandro Dalcin 1267277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1268277b19d0SLisandro Dalcin @*/ 1269277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1270277b19d0SLisandro Dalcin { 1271277b19d0SLisandro Dalcin PetscErrorCode ierr; 1272277b19d0SLisandro Dalcin 1273277b19d0SLisandro Dalcin PetscFunctionBegin; 1274277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1275277b19d0SLisandro Dalcin if (ts->ops->reset) { 1276277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1277277b19d0SLisandro Dalcin } 1278277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12796bf464f9SBarry Smith ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr); 1280089b2837SJed Brown ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr); 12816bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1282277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1283277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1284d763cef2SBarry Smith PetscFunctionReturn(0); 1285d763cef2SBarry Smith } 1286d763cef2SBarry Smith 12874a2ae208SSatish Balay #undef __FUNCT__ 12884a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1289d8e5e3e6SSatish Balay /*@ 1290d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1291d763cef2SBarry Smith with TSCreate(). 1292d763cef2SBarry Smith 1293d763cef2SBarry Smith Collective on TS 1294d763cef2SBarry Smith 1295d763cef2SBarry Smith Input Parameter: 1296d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1297d763cef2SBarry Smith 1298d763cef2SBarry Smith Level: beginner 1299d763cef2SBarry Smith 1300d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1301d763cef2SBarry Smith 1302d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1303d763cef2SBarry Smith @*/ 13046bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1305d763cef2SBarry Smith { 13066849ba73SBarry Smith PetscErrorCode ierr; 1307d763cef2SBarry Smith 1308d763cef2SBarry Smith PetscFunctionBegin; 13096bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 13106bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 13116bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1312d763cef2SBarry Smith 13136bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1314277b19d0SLisandro Dalcin 1315be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13166bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 13176bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 13186d4c513bSLisandro Dalcin 13196bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 13206bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 13216bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 13226d4c513bSLisandro Dalcin 1323a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1324d763cef2SBarry Smith PetscFunctionReturn(0); 1325d763cef2SBarry Smith } 1326d763cef2SBarry Smith 13274a2ae208SSatish Balay #undef __FUNCT__ 13284a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1329d8e5e3e6SSatish Balay /*@ 1330d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1331d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1332d763cef2SBarry Smith 1333d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1334d763cef2SBarry Smith 1335d763cef2SBarry Smith Input Parameter: 1336d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1337d763cef2SBarry Smith 1338d763cef2SBarry Smith Output Parameter: 1339d763cef2SBarry Smith . snes - the nonlinear solver context 1340d763cef2SBarry Smith 1341d763cef2SBarry Smith Notes: 1342d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1343d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 134494b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1345d763cef2SBarry Smith 1346d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1347d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1348d763cef2SBarry Smith 1349d763cef2SBarry Smith Level: beginner 1350d763cef2SBarry Smith 1351d763cef2SBarry Smith .keywords: timestep, get, SNES 1352d763cef2SBarry Smith @*/ 13537087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1354d763cef2SBarry Smith { 1355d372ba47SLisandro Dalcin PetscErrorCode ierr; 1356d372ba47SLisandro Dalcin 1357d763cef2SBarry Smith PetscFunctionBegin; 13580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13594482741eSBarry Smith PetscValidPointer(snes,2); 1360d372ba47SLisandro Dalcin if (!ts->snes) { 1361d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1362d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1363d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1364*9e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 1365*9e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 1366*9e2a6581SJed Brown } 1367d372ba47SLisandro Dalcin } 1368d763cef2SBarry Smith *snes = ts->snes; 1369d763cef2SBarry Smith PetscFunctionReturn(0); 1370d763cef2SBarry Smith } 1371d763cef2SBarry Smith 13724a2ae208SSatish Balay #undef __FUNCT__ 137394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1374d8e5e3e6SSatish Balay /*@ 137594b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1376d763cef2SBarry Smith a TS (timestepper) context. 1377d763cef2SBarry Smith 137894b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1379d763cef2SBarry Smith 1380d763cef2SBarry Smith Input Parameter: 1381d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1382d763cef2SBarry Smith 1383d763cef2SBarry Smith Output Parameter: 138494b7f48cSBarry Smith . ksp - the nonlinear solver context 1385d763cef2SBarry Smith 1386d763cef2SBarry Smith Notes: 138794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1388d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1389d763cef2SBarry Smith KSP and PC contexts as well. 1390d763cef2SBarry Smith 139194b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 139294b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1393d763cef2SBarry Smith 1394d763cef2SBarry Smith Level: beginner 1395d763cef2SBarry Smith 139694b7f48cSBarry Smith .keywords: timestep, get, KSP 1397d763cef2SBarry Smith @*/ 13987087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1399d763cef2SBarry Smith { 1400d372ba47SLisandro Dalcin PetscErrorCode ierr; 1401089b2837SJed Brown SNES snes; 1402d372ba47SLisandro Dalcin 1403d763cef2SBarry Smith PetscFunctionBegin; 14040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14054482741eSBarry Smith PetscValidPointer(ksp,2); 140617186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1407e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1408089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1409089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1410d763cef2SBarry Smith PetscFunctionReturn(0); 1411d763cef2SBarry Smith } 1412d763cef2SBarry Smith 1413d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1414d763cef2SBarry Smith 14154a2ae208SSatish Balay #undef __FUNCT__ 1416adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1417adb62b0dSMatthew Knepley /*@ 1418adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1419adb62b0dSMatthew Knepley maximum time for iteration. 1420adb62b0dSMatthew Knepley 14213f9fe445SBarry Smith Not Collective 1422adb62b0dSMatthew Knepley 1423adb62b0dSMatthew Knepley Input Parameters: 1424adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1425adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1426adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1427adb62b0dSMatthew Knepley 1428adb62b0dSMatthew Knepley Level: intermediate 1429adb62b0dSMatthew Knepley 1430adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1431adb62b0dSMatthew Knepley @*/ 14327087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1433adb62b0dSMatthew Knepley { 1434adb62b0dSMatthew Knepley PetscFunctionBegin; 14350700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1436abc0a331SBarry Smith if (maxsteps) { 14374482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1438adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1439adb62b0dSMatthew Knepley } 1440abc0a331SBarry Smith if (maxtime ) { 14414482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1442adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1443adb62b0dSMatthew Knepley } 1444adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1445adb62b0dSMatthew Knepley } 1446adb62b0dSMatthew Knepley 1447adb62b0dSMatthew Knepley #undef __FUNCT__ 14484a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1449d763cef2SBarry Smith /*@ 1450d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1451d763cef2SBarry Smith maximum time for iteration. 1452d763cef2SBarry Smith 14533f9fe445SBarry Smith Logically Collective on TS 1454d763cef2SBarry Smith 1455d763cef2SBarry Smith Input Parameters: 1456d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1457d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1458d763cef2SBarry Smith - maxtime - final time to iterate to 1459d763cef2SBarry Smith 1460d763cef2SBarry Smith Options Database Keys: 1461d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1462d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1463d763cef2SBarry Smith 1464d763cef2SBarry Smith Notes: 1465d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1466d763cef2SBarry Smith 1467d763cef2SBarry Smith Level: intermediate 1468d763cef2SBarry Smith 1469d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1470d763cef2SBarry Smith @*/ 14717087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1472d763cef2SBarry Smith { 1473d763cef2SBarry Smith PetscFunctionBegin; 14740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1475c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1476c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1477d763cef2SBarry Smith ts->max_steps = maxsteps; 1478d763cef2SBarry Smith ts->max_time = maxtime; 1479d763cef2SBarry Smith PetscFunctionReturn(0); 1480d763cef2SBarry Smith } 1481d763cef2SBarry Smith 14824a2ae208SSatish Balay #undef __FUNCT__ 14834a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1484d763cef2SBarry Smith /*@ 1485d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1486d763cef2SBarry Smith for use by the TS routines. 1487d763cef2SBarry Smith 14883f9fe445SBarry Smith Logically Collective on TS and Vec 1489d763cef2SBarry Smith 1490d763cef2SBarry Smith Input Parameters: 1491d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1492d763cef2SBarry Smith - x - the solution vector 1493d763cef2SBarry Smith 1494d763cef2SBarry Smith Level: beginner 1495d763cef2SBarry Smith 1496d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1497d763cef2SBarry Smith @*/ 14987087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1499d763cef2SBarry Smith { 15008737fe31SLisandro Dalcin PetscErrorCode ierr; 15018737fe31SLisandro Dalcin 1502d763cef2SBarry Smith PetscFunctionBegin; 15030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15040700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 15058737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 15066bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 15078737fe31SLisandro Dalcin ts->vec_sol = x; 1508d763cef2SBarry Smith PetscFunctionReturn(0); 1509d763cef2SBarry Smith } 1510d763cef2SBarry Smith 1511e74ef692SMatthew Knepley #undef __FUNCT__ 1512e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1513ac226902SBarry Smith /*@C 1514000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 15153f2090d5SJed Brown called once at the beginning of each time step. 1516000e7ae3SMatthew Knepley 15173f9fe445SBarry Smith Logically Collective on TS 1518000e7ae3SMatthew Knepley 1519000e7ae3SMatthew Knepley Input Parameters: 1520000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1521000e7ae3SMatthew Knepley - func - The function 1522000e7ae3SMatthew Knepley 1523000e7ae3SMatthew Knepley Calling sequence of func: 1524000e7ae3SMatthew Knepley . func (TS ts); 1525000e7ae3SMatthew Knepley 1526000e7ae3SMatthew Knepley Level: intermediate 1527000e7ae3SMatthew Knepley 1528000e7ae3SMatthew Knepley .keywords: TS, timestep 1529000e7ae3SMatthew Knepley @*/ 15307087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1531000e7ae3SMatthew Knepley { 1532000e7ae3SMatthew Knepley PetscFunctionBegin; 15330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1534000e7ae3SMatthew Knepley ts->ops->prestep = func; 1535000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1536000e7ae3SMatthew Knepley } 1537000e7ae3SMatthew Knepley 1538e74ef692SMatthew Knepley #undef __FUNCT__ 15393f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 15403f2090d5SJed Brown /*@C 15413f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 15423f2090d5SJed Brown 15433f2090d5SJed Brown Collective on TS 15443f2090d5SJed Brown 15453f2090d5SJed Brown Input Parameters: 15463f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15473f2090d5SJed Brown 15483f2090d5SJed Brown Notes: 15493f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 15503f2090d5SJed Brown so most users would not generally call this routine themselves. 15513f2090d5SJed Brown 15523f2090d5SJed Brown Level: developer 15533f2090d5SJed Brown 15543f2090d5SJed Brown .keywords: TS, timestep 15553f2090d5SJed Brown @*/ 15567087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 15573f2090d5SJed Brown { 15583f2090d5SJed Brown PetscErrorCode ierr; 15593f2090d5SJed Brown 15603f2090d5SJed Brown PetscFunctionBegin; 15610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 156272ac3e02SJed Brown if (ts->ops->prestep) { 15633f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15643f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15653f2090d5SJed Brown PetscStackPop; 1566312ce896SJed Brown } 15673f2090d5SJed Brown PetscFunctionReturn(0); 15683f2090d5SJed Brown } 15693f2090d5SJed Brown 15703f2090d5SJed Brown #undef __FUNCT__ 1571e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1572ac226902SBarry Smith /*@C 1573000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15743f2090d5SJed Brown called once at the end of each time step. 1575000e7ae3SMatthew Knepley 15763f9fe445SBarry Smith Logically Collective on TS 1577000e7ae3SMatthew Knepley 1578000e7ae3SMatthew Knepley Input Parameters: 1579000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1580000e7ae3SMatthew Knepley - func - The function 1581000e7ae3SMatthew Knepley 1582000e7ae3SMatthew Knepley Calling sequence of func: 1583000e7ae3SMatthew Knepley . func (TS ts); 1584000e7ae3SMatthew Knepley 1585000e7ae3SMatthew Knepley Level: intermediate 1586000e7ae3SMatthew Knepley 1587000e7ae3SMatthew Knepley .keywords: TS, timestep 1588000e7ae3SMatthew Knepley @*/ 15897087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1590000e7ae3SMatthew Knepley { 1591000e7ae3SMatthew Knepley PetscFunctionBegin; 15920700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1593000e7ae3SMatthew Knepley ts->ops->poststep = func; 1594000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1595000e7ae3SMatthew Knepley } 1596000e7ae3SMatthew Knepley 1597e74ef692SMatthew Knepley #undef __FUNCT__ 15983f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15993f2090d5SJed Brown /*@C 16003f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 16013f2090d5SJed Brown 16023f2090d5SJed Brown Collective on TS 16033f2090d5SJed Brown 16043f2090d5SJed Brown Input Parameters: 16053f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 16063f2090d5SJed Brown 16073f2090d5SJed Brown Notes: 16083f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 16093f2090d5SJed Brown so most users would not generally call this routine themselves. 16103f2090d5SJed Brown 16113f2090d5SJed Brown Level: developer 16123f2090d5SJed Brown 16133f2090d5SJed Brown .keywords: TS, timestep 16143f2090d5SJed Brown @*/ 16157087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 16163f2090d5SJed Brown { 16173f2090d5SJed Brown PetscErrorCode ierr; 16183f2090d5SJed Brown 16193f2090d5SJed Brown PetscFunctionBegin; 16200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 162172ac3e02SJed Brown if (ts->ops->poststep) { 16223f2090d5SJed Brown PetscStackPush("TS PostStep function"); 16233f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 16243f2090d5SJed Brown PetscStackPop; 162572ac3e02SJed Brown } 16263f2090d5SJed Brown PetscFunctionReturn(0); 16273f2090d5SJed Brown } 16283f2090d5SJed Brown 1629d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1630d763cef2SBarry Smith 16314a2ae208SSatish Balay #undef __FUNCT__ 1632a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1633d763cef2SBarry Smith /*@C 1634a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1635d763cef2SBarry Smith timestep to display the iteration's progress. 1636d763cef2SBarry Smith 16373f9fe445SBarry Smith Logically Collective on TS 1638d763cef2SBarry Smith 1639d763cef2SBarry Smith Input Parameters: 1640d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1641d763cef2SBarry Smith . func - monitoring routine 1642329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1643b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1644b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1645b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1646d763cef2SBarry Smith 1647d763cef2SBarry Smith Calling sequence of func: 1648a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith + ts - the TS context 1651d763cef2SBarry Smith . steps - iteration number 16521f06c33eSBarry Smith . time - current time 1653d763cef2SBarry Smith . x - current iterate 1654d763cef2SBarry Smith - mctx - [optional] monitoring context 1655d763cef2SBarry Smith 1656d763cef2SBarry Smith Notes: 1657d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1658d763cef2SBarry Smith already has been loaded. 1659d763cef2SBarry Smith 1660025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1661025f1a04SBarry Smith 1662d763cef2SBarry Smith Level: intermediate 1663d763cef2SBarry Smith 1664d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1665d763cef2SBarry Smith 1666a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1667d763cef2SBarry Smith @*/ 1668c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1669d763cef2SBarry Smith { 1670d763cef2SBarry Smith PetscFunctionBegin; 16710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 167217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1673d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1674329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1675d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1676d763cef2SBarry Smith PetscFunctionReturn(0); 1677d763cef2SBarry Smith } 1678d763cef2SBarry Smith 16794a2ae208SSatish Balay #undef __FUNCT__ 1680a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1681d763cef2SBarry Smith /*@C 1682a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1683d763cef2SBarry Smith 16843f9fe445SBarry Smith Logically Collective on TS 1685d763cef2SBarry Smith 1686d763cef2SBarry Smith Input Parameters: 1687d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith Notes: 1690d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1691d763cef2SBarry Smith 1692d763cef2SBarry Smith Level: intermediate 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1695d763cef2SBarry Smith 1696a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1697d763cef2SBarry Smith @*/ 16987087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1699d763cef2SBarry Smith { 1700d952e501SBarry Smith PetscErrorCode ierr; 1701d952e501SBarry Smith PetscInt i; 1702d952e501SBarry Smith 1703d763cef2SBarry Smith PetscFunctionBegin; 17040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1705d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1706d952e501SBarry Smith if (ts->mdestroy[i]) { 17073c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1708d952e501SBarry Smith } 1709d952e501SBarry Smith } 1710d763cef2SBarry Smith ts->numbermonitors = 0; 1711d763cef2SBarry Smith PetscFunctionReturn(0); 1712d763cef2SBarry Smith } 1713d763cef2SBarry Smith 17144a2ae208SSatish Balay #undef __FUNCT__ 1715a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1716d8e5e3e6SSatish Balay /*@ 1717a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 17185516499fSSatish Balay 17195516499fSSatish Balay Level: intermediate 172041251cbbSSatish Balay 17215516499fSSatish Balay .keywords: TS, set, monitor 17225516499fSSatish Balay 172341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 172441251cbbSSatish Balay @*/ 1725649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1726d763cef2SBarry Smith { 1727dfbe8321SBarry Smith PetscErrorCode ierr; 1728649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1729d132466eSBarry Smith 1730d763cef2SBarry Smith PetscFunctionBegin; 1731649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1732649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1733649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1734d763cef2SBarry Smith PetscFunctionReturn(0); 1735d763cef2SBarry Smith } 1736d763cef2SBarry Smith 17374a2ae208SSatish Balay #undef __FUNCT__ 17384a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1739d763cef2SBarry Smith /*@ 1740d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1741d763cef2SBarry Smith 1742d763cef2SBarry Smith Collective on TS 1743d763cef2SBarry Smith 1744d763cef2SBarry Smith Input Parameter: 1745d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1746d763cef2SBarry Smith 1747d763cef2SBarry Smith Output Parameters: 1748d763cef2SBarry Smith + steps - number of iterations until termination 1749142b95e3SSatish Balay - ptime - time until termination 1750d763cef2SBarry Smith 1751d763cef2SBarry Smith Level: beginner 1752d763cef2SBarry Smith 1753d763cef2SBarry Smith .keywords: TS, timestep, solve 1754d763cef2SBarry Smith 1755d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1756d763cef2SBarry Smith @*/ 17577087cfbeSBarry Smith PetscErrorCode TSStep(TS ts,PetscInt *steps,PetscReal *ptime) 1758d763cef2SBarry Smith { 1759dfbe8321SBarry Smith PetscErrorCode ierr; 1760d763cef2SBarry Smith 1761d763cef2SBarry Smith PetscFunctionBegin; 17620700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1763277b19d0SLisandro Dalcin 1764d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1765d405a339SMatthew Knepley 1766d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1767000e7ae3SMatthew Knepley ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr); 1768d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1769d405a339SMatthew Knepley 17704bb05414SBarry Smith if (!PetscPreLoadingOn) { 17717adad957SLisandro Dalcin ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1772d405a339SMatthew Knepley } 1773d763cef2SBarry Smith PetscFunctionReturn(0); 1774d763cef2SBarry Smith } 1775d763cef2SBarry Smith 17764a2ae208SSatish Balay #undef __FUNCT__ 17776a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17786a4d4014SLisandro Dalcin /*@ 17796a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17806a4d4014SLisandro Dalcin 17816a4d4014SLisandro Dalcin Collective on TS 17826a4d4014SLisandro Dalcin 17836a4d4014SLisandro Dalcin Input Parameter: 17846a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17856a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17866a4d4014SLisandro Dalcin 17876a4d4014SLisandro Dalcin Level: beginner 17886a4d4014SLisandro Dalcin 17896a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17906a4d4014SLisandro Dalcin 17916a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17926a4d4014SLisandro Dalcin @*/ 17937087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17946a4d4014SLisandro Dalcin { 17956a4d4014SLisandro Dalcin PetscInt steps; 17966a4d4014SLisandro Dalcin PetscReal ptime; 17976a4d4014SLisandro Dalcin PetscErrorCode ierr; 1798f22f69f0SBarry Smith 17996a4d4014SLisandro Dalcin PetscFunctionBegin; 18000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18016a4d4014SLisandro Dalcin /* set solution vector if provided */ 18026a4d4014SLisandro Dalcin if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); } 18036a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 18046a4d4014SLisandro Dalcin ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0; 18056a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 18066a4d4014SLisandro Dalcin ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr); 18076a4d4014SLisandro Dalcin PetscFunctionReturn(0); 18086a4d4014SLisandro Dalcin } 18096a4d4014SLisandro Dalcin 18106a4d4014SLisandro Dalcin #undef __FUNCT__ 18114a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1812d763cef2SBarry Smith /* 1813d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1814d763cef2SBarry Smith */ 1815a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1816d763cef2SBarry Smith { 18176849ba73SBarry Smith PetscErrorCode ierr; 1818a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1819d763cef2SBarry Smith 1820d763cef2SBarry Smith PetscFunctionBegin; 1821d763cef2SBarry Smith for (i=0; i<n; i++) { 1822142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1823d763cef2SBarry Smith } 1824d763cef2SBarry Smith PetscFunctionReturn(0); 1825d763cef2SBarry Smith } 1826d763cef2SBarry Smith 1827d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1828d763cef2SBarry Smith 18294a2ae208SSatish Balay #undef __FUNCT__ 1830a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1831d763cef2SBarry Smith /*@C 1832a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1833d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1834d763cef2SBarry Smith 1835d763cef2SBarry Smith Collective on TS 1836d763cef2SBarry Smith 1837d763cef2SBarry Smith Input Parameters: 1838d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1839d763cef2SBarry Smith . label - the title to put in the title bar 18407c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1841d763cef2SBarry Smith - m, n - the screen width and height in pixels 1842d763cef2SBarry Smith 1843d763cef2SBarry Smith Output Parameter: 1844d763cef2SBarry Smith . draw - the drawing context 1845d763cef2SBarry Smith 1846d763cef2SBarry Smith Options Database Key: 1847a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1848d763cef2SBarry Smith 1849d763cef2SBarry Smith Notes: 1850a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1851d763cef2SBarry Smith 1852d763cef2SBarry Smith Level: intermediate 1853d763cef2SBarry Smith 18547c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1855d763cef2SBarry Smith 1856a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18577c922b88SBarry Smith 1858d763cef2SBarry Smith @*/ 18597087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1860d763cef2SBarry Smith { 1861b0a32e0cSBarry Smith PetscDraw win; 1862dfbe8321SBarry Smith PetscErrorCode ierr; 1863d763cef2SBarry Smith 1864d763cef2SBarry Smith PetscFunctionBegin; 1865b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1866b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1867b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1868b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1869d763cef2SBarry Smith 187052e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1871d763cef2SBarry Smith PetscFunctionReturn(0); 1872d763cef2SBarry Smith } 1873d763cef2SBarry Smith 18744a2ae208SSatish Balay #undef __FUNCT__ 1875a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1876a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1877d763cef2SBarry Smith { 1878b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 187987828ca2SBarry Smith PetscReal x,y = ptime; 1880dfbe8321SBarry Smith PetscErrorCode ierr; 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith PetscFunctionBegin; 18837c922b88SBarry Smith if (!monctx) { 18847c922b88SBarry Smith MPI_Comm comm; 1885b0a32e0cSBarry Smith PetscViewer viewer; 18867c922b88SBarry Smith 18877c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1888b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1889b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18907c922b88SBarry Smith } 18917c922b88SBarry Smith 1892b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 189387828ca2SBarry Smith x = (PetscReal)n; 1894b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1895d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1896b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1897d763cef2SBarry Smith } 1898d763cef2SBarry Smith PetscFunctionReturn(0); 1899d763cef2SBarry Smith } 1900d763cef2SBarry Smith 19014a2ae208SSatish Balay #undef __FUNCT__ 1902a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1903d763cef2SBarry Smith /*@C 1904a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1905a6570f20SBarry Smith with TSMonitorLGCreate(). 1906d763cef2SBarry Smith 1907b0a32e0cSBarry Smith Collective on PetscDrawLG 1908d763cef2SBarry Smith 1909d763cef2SBarry Smith Input Parameter: 1910d763cef2SBarry Smith . draw - the drawing context 1911d763cef2SBarry Smith 1912d763cef2SBarry Smith Level: intermediate 1913d763cef2SBarry Smith 1914d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1915d763cef2SBarry Smith 1916a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1917d763cef2SBarry Smith @*/ 19186bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1919d763cef2SBarry Smith { 1920b0a32e0cSBarry Smith PetscDraw draw; 1921dfbe8321SBarry Smith PetscErrorCode ierr; 1922d763cef2SBarry Smith 1923d763cef2SBarry Smith PetscFunctionBegin; 19246bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 19256bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1926b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1927d763cef2SBarry Smith PetscFunctionReturn(0); 1928d763cef2SBarry Smith } 1929d763cef2SBarry Smith 19304a2ae208SSatish Balay #undef __FUNCT__ 19314a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1932d763cef2SBarry Smith /*@ 1933d763cef2SBarry Smith TSGetTime - Gets the current time. 1934d763cef2SBarry Smith 1935d763cef2SBarry Smith Not Collective 1936d763cef2SBarry Smith 1937d763cef2SBarry Smith Input Parameter: 1938d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1939d763cef2SBarry Smith 1940d763cef2SBarry Smith Output Parameter: 1941d763cef2SBarry Smith . t - the current time 1942d763cef2SBarry Smith 1943d763cef2SBarry Smith Level: beginner 1944d763cef2SBarry Smith 1945d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1946d763cef2SBarry Smith 1947d763cef2SBarry Smith .keywords: TS, get, time 1948d763cef2SBarry Smith @*/ 19497087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1950d763cef2SBarry Smith { 1951d763cef2SBarry Smith PetscFunctionBegin; 19520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19534482741eSBarry Smith PetscValidDoublePointer(t,2); 1954d763cef2SBarry Smith *t = ts->ptime; 1955d763cef2SBarry Smith PetscFunctionReturn(0); 1956d763cef2SBarry Smith } 1957d763cef2SBarry Smith 19584a2ae208SSatish Balay #undef __FUNCT__ 19596a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19606a4d4014SLisandro Dalcin /*@ 19616a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19626a4d4014SLisandro Dalcin 19633f9fe445SBarry Smith Logically Collective on TS 19646a4d4014SLisandro Dalcin 19656a4d4014SLisandro Dalcin Input Parameters: 19666a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19676a4d4014SLisandro Dalcin - time - the time 19686a4d4014SLisandro Dalcin 19696a4d4014SLisandro Dalcin Level: intermediate 19706a4d4014SLisandro Dalcin 19716a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19726a4d4014SLisandro Dalcin 19736a4d4014SLisandro Dalcin .keywords: TS, set, time 19746a4d4014SLisandro Dalcin @*/ 19757087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19766a4d4014SLisandro Dalcin { 19776a4d4014SLisandro Dalcin PetscFunctionBegin; 19780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1979c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19806a4d4014SLisandro Dalcin ts->ptime = t; 19816a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19826a4d4014SLisandro Dalcin } 19836a4d4014SLisandro Dalcin 19846a4d4014SLisandro Dalcin #undef __FUNCT__ 19854a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1986d763cef2SBarry Smith /*@C 1987d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1988d763cef2SBarry Smith TS options in the database. 1989d763cef2SBarry Smith 19903f9fe445SBarry Smith Logically Collective on TS 1991d763cef2SBarry Smith 1992d763cef2SBarry Smith Input Parameter: 1993d763cef2SBarry Smith + ts - The TS context 1994d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1995d763cef2SBarry Smith 1996d763cef2SBarry Smith Notes: 1997d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1998d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1999d763cef2SBarry Smith hyphen. 2000d763cef2SBarry Smith 2001d763cef2SBarry Smith Level: advanced 2002d763cef2SBarry Smith 2003d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith .seealso: TSSetFromOptions() 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith @*/ 20087087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2009d763cef2SBarry Smith { 2010dfbe8321SBarry Smith PetscErrorCode ierr; 2011089b2837SJed Brown SNES snes; 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith PetscFunctionBegin; 20140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2015d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2016089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2017089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2018d763cef2SBarry Smith PetscFunctionReturn(0); 2019d763cef2SBarry Smith } 2020d763cef2SBarry Smith 2021d763cef2SBarry Smith 20224a2ae208SSatish Balay #undef __FUNCT__ 20234a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2024d763cef2SBarry Smith /*@C 2025d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2026d763cef2SBarry Smith TS options in the database. 2027d763cef2SBarry Smith 20283f9fe445SBarry Smith Logically Collective on TS 2029d763cef2SBarry Smith 2030d763cef2SBarry Smith Input Parameter: 2031d763cef2SBarry Smith + ts - The TS context 2032d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2033d763cef2SBarry Smith 2034d763cef2SBarry Smith Notes: 2035d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2036d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2037d763cef2SBarry Smith hyphen. 2038d763cef2SBarry Smith 2039d763cef2SBarry Smith Level: advanced 2040d763cef2SBarry Smith 2041d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2044d763cef2SBarry Smith 2045d763cef2SBarry Smith @*/ 20467087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2047d763cef2SBarry Smith { 2048dfbe8321SBarry Smith PetscErrorCode ierr; 2049089b2837SJed Brown SNES snes; 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith PetscFunctionBegin; 20520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2053d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2054089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2055089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2056d763cef2SBarry Smith PetscFunctionReturn(0); 2057d763cef2SBarry Smith } 2058d763cef2SBarry Smith 20594a2ae208SSatish Balay #undef __FUNCT__ 20604a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2061d763cef2SBarry Smith /*@C 2062d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2063d763cef2SBarry Smith TS options in the database. 2064d763cef2SBarry Smith 2065d763cef2SBarry Smith Not Collective 2066d763cef2SBarry Smith 2067d763cef2SBarry Smith Input Parameter: 2068d763cef2SBarry Smith . ts - The TS context 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith Output Parameter: 2071d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2074d763cef2SBarry Smith sufficient length to hold the prefix. 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith Level: intermediate 2077d763cef2SBarry Smith 2078d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2079d763cef2SBarry Smith 2080d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2081d763cef2SBarry Smith @*/ 20827087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2083d763cef2SBarry Smith { 2084dfbe8321SBarry Smith PetscErrorCode ierr; 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith PetscFunctionBegin; 20870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20884482741eSBarry Smith PetscValidPointer(prefix,2); 2089d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2090d763cef2SBarry Smith PetscFunctionReturn(0); 2091d763cef2SBarry Smith } 2092d763cef2SBarry Smith 20934a2ae208SSatish Balay #undef __FUNCT__ 20944a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2095d763cef2SBarry Smith /*@C 2096d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2099d763cef2SBarry Smith 2100d763cef2SBarry Smith Input Parameter: 2101d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2102d763cef2SBarry Smith 2103d763cef2SBarry Smith Output Parameters: 2104d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2105d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2106089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2107d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2108d763cef2SBarry Smith 2109d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith Level: intermediate 2112d763cef2SBarry Smith 211326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2114d763cef2SBarry Smith 2115d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2116d763cef2SBarry Smith @*/ 2117089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2118d763cef2SBarry Smith { 2119089b2837SJed Brown PetscErrorCode ierr; 2120089b2837SJed Brown SNES snes; 2121089b2837SJed Brown 2122d763cef2SBarry Smith PetscFunctionBegin; 2123089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2124089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2125089b2837SJed Brown if (func) *func = ts->ops->rhsjacobian; 212626d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2127d763cef2SBarry Smith PetscFunctionReturn(0); 2128d763cef2SBarry Smith } 2129d763cef2SBarry Smith 21301713a123SBarry Smith #undef __FUNCT__ 21312eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 21322eca1d9cSJed Brown /*@C 21332eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 21342eca1d9cSJed Brown 21352eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 21362eca1d9cSJed Brown 21372eca1d9cSJed Brown Input Parameter: 21382eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 21392eca1d9cSJed Brown 21402eca1d9cSJed Brown Output Parameters: 21412eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 21422eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 21432eca1d9cSJed Brown . f - The function to compute the matrices 21442eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 21452eca1d9cSJed Brown 21462eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 21472eca1d9cSJed Brown 21482eca1d9cSJed Brown Level: advanced 21492eca1d9cSJed Brown 21502eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 21512eca1d9cSJed Brown 21522eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 21532eca1d9cSJed Brown @*/ 21547087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21552eca1d9cSJed Brown { 2156089b2837SJed Brown PetscErrorCode ierr; 2157089b2837SJed Brown SNES snes; 2158089b2837SJed Brown 21592eca1d9cSJed Brown PetscFunctionBegin; 2160089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2161089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21622eca1d9cSJed Brown if (f) *f = ts->ops->ijacobian; 21632eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21642eca1d9cSJed Brown PetscFunctionReturn(0); 21652eca1d9cSJed Brown } 21662eca1d9cSJed Brown 21672eca1d9cSJed Brown #undef __FUNCT__ 2168a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21691713a123SBarry Smith /*@C 2170a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21711713a123SBarry Smith VecView() for the solution at each timestep 21721713a123SBarry Smith 21731713a123SBarry Smith Collective on TS 21741713a123SBarry Smith 21751713a123SBarry Smith Input Parameters: 21761713a123SBarry Smith + ts - the TS context 21771713a123SBarry Smith . step - current time-step 2178142b95e3SSatish Balay . ptime - current time 21791713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21801713a123SBarry Smith 21811713a123SBarry Smith Level: intermediate 21821713a123SBarry Smith 21831713a123SBarry Smith .keywords: TS, vector, monitor, view 21841713a123SBarry Smith 2185a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21861713a123SBarry Smith @*/ 21877087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21881713a123SBarry Smith { 2189dfbe8321SBarry Smith PetscErrorCode ierr; 21901713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21911713a123SBarry Smith 21921713a123SBarry Smith PetscFunctionBegin; 2193a34d58ebSBarry Smith if (!dummy) { 21947adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21951713a123SBarry Smith } 21961713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21971713a123SBarry Smith PetscFunctionReturn(0); 21981713a123SBarry Smith } 21991713a123SBarry Smith 22001713a123SBarry Smith 22016c699258SBarry Smith #undef __FUNCT__ 22026c699258SBarry Smith #define __FUNCT__ "TSSetDM" 22036c699258SBarry Smith /*@ 22046c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 22056c699258SBarry Smith 22063f9fe445SBarry Smith Logically Collective on TS and DM 22076c699258SBarry Smith 22086c699258SBarry Smith Input Parameters: 22096c699258SBarry Smith + ts - the preconditioner context 22106c699258SBarry Smith - dm - the dm 22116c699258SBarry Smith 22126c699258SBarry Smith Level: intermediate 22136c699258SBarry Smith 22146c699258SBarry Smith 22156c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 22166c699258SBarry Smith @*/ 22177087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 22186c699258SBarry Smith { 22196c699258SBarry Smith PetscErrorCode ierr; 2220089b2837SJed Brown SNES snes; 22216c699258SBarry Smith 22226c699258SBarry Smith PetscFunctionBegin; 22230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 222470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 22256bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 22266c699258SBarry Smith ts->dm = dm; 2227089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2228089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 22296c699258SBarry Smith PetscFunctionReturn(0); 22306c699258SBarry Smith } 22316c699258SBarry Smith 22326c699258SBarry Smith #undef __FUNCT__ 22336c699258SBarry Smith #define __FUNCT__ "TSGetDM" 22346c699258SBarry Smith /*@ 22356c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 22366c699258SBarry Smith 22373f9fe445SBarry Smith Not Collective 22386c699258SBarry Smith 22396c699258SBarry Smith Input Parameter: 22406c699258SBarry Smith . ts - the preconditioner context 22416c699258SBarry Smith 22426c699258SBarry Smith Output Parameter: 22436c699258SBarry Smith . dm - the dm 22446c699258SBarry Smith 22456c699258SBarry Smith Level: intermediate 22466c699258SBarry Smith 22476c699258SBarry Smith 22486c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 22496c699258SBarry Smith @*/ 22507087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 22516c699258SBarry Smith { 22526c699258SBarry Smith PetscFunctionBegin; 22530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22546c699258SBarry Smith *dm = ts->dm; 22556c699258SBarry Smith PetscFunctionReturn(0); 22566c699258SBarry Smith } 22571713a123SBarry Smith 22580f5c6efeSJed Brown #undef __FUNCT__ 22590f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22600f5c6efeSJed Brown /*@ 22610f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22620f5c6efeSJed Brown 22633f9fe445SBarry Smith Logically Collective on SNES 22640f5c6efeSJed Brown 22650f5c6efeSJed Brown Input Parameter: 2266d42a1c89SJed Brown + snes - nonlinear solver 22670f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2268d42a1c89SJed Brown - ctx - user context, must be a TS 22690f5c6efeSJed Brown 22700f5c6efeSJed Brown Output Parameter: 22710f5c6efeSJed Brown . F - the nonlinear residual 22720f5c6efeSJed Brown 22730f5c6efeSJed Brown Notes: 22740f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22750f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22760f5c6efeSJed Brown 22770f5c6efeSJed Brown Level: advanced 22780f5c6efeSJed Brown 22790f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22800f5c6efeSJed Brown @*/ 22817087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22820f5c6efeSJed Brown { 22830f5c6efeSJed Brown TS ts = (TS)ctx; 22840f5c6efeSJed Brown PetscErrorCode ierr; 22850f5c6efeSJed Brown 22860f5c6efeSJed Brown PetscFunctionBegin; 22870f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22880f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22890f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22900f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22910f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22920f5c6efeSJed Brown PetscFunctionReturn(0); 22930f5c6efeSJed Brown } 22940f5c6efeSJed Brown 22950f5c6efeSJed Brown #undef __FUNCT__ 22960f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22970f5c6efeSJed Brown /*@ 22980f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22990f5c6efeSJed Brown 23000f5c6efeSJed Brown Collective on SNES 23010f5c6efeSJed Brown 23020f5c6efeSJed Brown Input Parameter: 23030f5c6efeSJed Brown + snes - nonlinear solver 23040f5c6efeSJed Brown . X - the current state at which to evaluate the residual 23050f5c6efeSJed Brown - ctx - user context, must be a TS 23060f5c6efeSJed Brown 23070f5c6efeSJed Brown Output Parameter: 23080f5c6efeSJed Brown + A - the Jacobian 23090f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 23100f5c6efeSJed Brown - flag - indicates any structure change in the matrix 23110f5c6efeSJed Brown 23120f5c6efeSJed Brown Notes: 23130f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 23140f5c6efeSJed Brown 23150f5c6efeSJed Brown Level: developer 23160f5c6efeSJed Brown 23170f5c6efeSJed Brown .seealso: SNESSetJacobian() 23180f5c6efeSJed Brown @*/ 23197087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 23200f5c6efeSJed Brown { 23210f5c6efeSJed Brown TS ts = (TS)ctx; 23220f5c6efeSJed Brown PetscErrorCode ierr; 23230f5c6efeSJed Brown 23240f5c6efeSJed Brown PetscFunctionBegin; 23250f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23260f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 23270f5c6efeSJed Brown PetscValidPointer(A,3); 23280f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 23290f5c6efeSJed Brown PetscValidPointer(B,4); 23300f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 23310f5c6efeSJed Brown PetscValidPointer(flag,5); 23320f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 23330f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 23340f5c6efeSJed Brown PetscFunctionReturn(0); 23350f5c6efeSJed Brown } 2336325fc9f4SBarry Smith 2337325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2338c6db04a5SJed Brown #include <mex.h> 2339325fc9f4SBarry Smith 2340325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2341325fc9f4SBarry Smith 2342325fc9f4SBarry Smith #undef __FUNCT__ 2343325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2344325fc9f4SBarry Smith /* 2345325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2346325fc9f4SBarry Smith TSSetFunctionMatlab(). 2347325fc9f4SBarry Smith 2348325fc9f4SBarry Smith Collective on TS 2349325fc9f4SBarry Smith 2350325fc9f4SBarry Smith Input Parameters: 2351325fc9f4SBarry Smith + snes - the TS context 2352325fc9f4SBarry Smith - x - input vector 2353325fc9f4SBarry Smith 2354325fc9f4SBarry Smith Output Parameter: 2355325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2356325fc9f4SBarry Smith 2357325fc9f4SBarry Smith Notes: 2358325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2359325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2360325fc9f4SBarry Smith themselves. 2361325fc9f4SBarry Smith 2362325fc9f4SBarry Smith Level: developer 2363325fc9f4SBarry Smith 2364325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2365325fc9f4SBarry Smith 2366325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2367325fc9f4SBarry Smith */ 23687087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2369325fc9f4SBarry Smith { 2370325fc9f4SBarry Smith PetscErrorCode ierr; 2371325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2372325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2373325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2374325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2375325fc9f4SBarry Smith 2376325fc9f4SBarry Smith PetscFunctionBegin; 2377325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2378325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2379325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2380325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2381325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2382325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2383325fc9f4SBarry Smith 2384325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2385325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2386880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2387325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2388325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2389325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2390325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2391325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2392325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2393325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2394325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2395325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2396325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2397325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2398325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2399325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2400325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2401325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2402325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2403325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2404325fc9f4SBarry Smith PetscFunctionReturn(0); 2405325fc9f4SBarry Smith } 2406325fc9f4SBarry Smith 2407325fc9f4SBarry Smith 2408325fc9f4SBarry Smith #undef __FUNCT__ 2409325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2410325fc9f4SBarry Smith /* 2411325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2412325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2413e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2414325fc9f4SBarry Smith 2415325fc9f4SBarry Smith Logically Collective on TS 2416325fc9f4SBarry Smith 2417325fc9f4SBarry Smith Input Parameters: 2418325fc9f4SBarry Smith + ts - the TS context 2419325fc9f4SBarry Smith - func - function evaluation routine 2420325fc9f4SBarry Smith 2421325fc9f4SBarry Smith Calling sequence of func: 2422325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2423325fc9f4SBarry Smith 2424325fc9f4SBarry Smith Level: beginner 2425325fc9f4SBarry Smith 2426325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2427325fc9f4SBarry Smith 2428325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2429325fc9f4SBarry Smith */ 24307087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2431325fc9f4SBarry Smith { 2432325fc9f4SBarry Smith PetscErrorCode ierr; 2433325fc9f4SBarry Smith TSMatlabContext *sctx; 2434325fc9f4SBarry Smith 2435325fc9f4SBarry Smith PetscFunctionBegin; 2436325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2437325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2438325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2439325fc9f4SBarry Smith /* 2440325fc9f4SBarry Smith This should work, but it doesn't 2441325fc9f4SBarry Smith sctx->ctx = ctx; 2442325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2443325fc9f4SBarry Smith */ 2444325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2445325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2446325fc9f4SBarry Smith PetscFunctionReturn(0); 2447325fc9f4SBarry Smith } 2448325fc9f4SBarry Smith 2449325fc9f4SBarry Smith #undef __FUNCT__ 2450325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2451325fc9f4SBarry Smith /* 2452325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2453325fc9f4SBarry Smith TSSetJacobianMatlab(). 2454325fc9f4SBarry Smith 2455325fc9f4SBarry Smith Collective on TS 2456325fc9f4SBarry Smith 2457325fc9f4SBarry Smith Input Parameters: 2458325fc9f4SBarry Smith + snes - the TS context 2459325fc9f4SBarry Smith . x - input vector 2460325fc9f4SBarry Smith . A, B - the matrices 2461325fc9f4SBarry Smith - ctx - user context 2462325fc9f4SBarry Smith 2463325fc9f4SBarry Smith Output Parameter: 2464325fc9f4SBarry Smith . flag - structure of the matrix 2465325fc9f4SBarry Smith 2466325fc9f4SBarry Smith Level: developer 2467325fc9f4SBarry Smith 2468325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2469325fc9f4SBarry Smith 2470325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2471325fc9f4SBarry Smith @*/ 24727087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2473325fc9f4SBarry Smith { 2474325fc9f4SBarry Smith PetscErrorCode ierr; 2475325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2476325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2477325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2478325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2479325fc9f4SBarry Smith 2480325fc9f4SBarry Smith PetscFunctionBegin; 2481325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2482325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2483325fc9f4SBarry Smith 2484325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2485325fc9f4SBarry Smith 2486325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2487325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2488325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2489325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2490325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2491325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2492325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2493325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2494325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2495325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2496325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2497325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2498325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2499325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2500325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2501325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2502325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2503325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2504325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2505325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2506325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2507325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2508325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2509325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2510325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2511325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2512325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2513325fc9f4SBarry Smith PetscFunctionReturn(0); 2514325fc9f4SBarry Smith } 2515325fc9f4SBarry Smith 2516325fc9f4SBarry Smith 2517325fc9f4SBarry Smith #undef __FUNCT__ 2518325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2519325fc9f4SBarry Smith /* 2520325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2521e3c5b3baSBarry 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 2522325fc9f4SBarry Smith 2523325fc9f4SBarry Smith Logically Collective on TS 2524325fc9f4SBarry Smith 2525325fc9f4SBarry Smith Input Parameters: 2526325fc9f4SBarry Smith + snes - the TS context 2527325fc9f4SBarry Smith . A,B - Jacobian matrices 2528325fc9f4SBarry Smith . func - function evaluation routine 2529325fc9f4SBarry Smith - ctx - user context 2530325fc9f4SBarry Smith 2531325fc9f4SBarry Smith Calling sequence of func: 2532325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2533325fc9f4SBarry Smith 2534325fc9f4SBarry Smith 2535325fc9f4SBarry Smith Level: developer 2536325fc9f4SBarry Smith 2537325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2538325fc9f4SBarry Smith 2539325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2540325fc9f4SBarry Smith */ 25417087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2542325fc9f4SBarry Smith { 2543325fc9f4SBarry Smith PetscErrorCode ierr; 2544325fc9f4SBarry Smith TSMatlabContext *sctx; 2545325fc9f4SBarry Smith 2546325fc9f4SBarry Smith PetscFunctionBegin; 2547325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2548325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2549325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2550325fc9f4SBarry Smith /* 2551325fc9f4SBarry Smith This should work, but it doesn't 2552325fc9f4SBarry Smith sctx->ctx = ctx; 2553325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2554325fc9f4SBarry Smith */ 2555325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2556325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2557325fc9f4SBarry Smith PetscFunctionReturn(0); 2558325fc9f4SBarry Smith } 2559325fc9f4SBarry Smith 2560b5b1a830SBarry Smith #undef __FUNCT__ 2561b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2562b5b1a830SBarry Smith /* 2563b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2564b5b1a830SBarry Smith 2565b5b1a830SBarry Smith Collective on TS 2566b5b1a830SBarry Smith 2567b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2568b5b1a830SBarry Smith @*/ 25697087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2570b5b1a830SBarry Smith { 2571b5b1a830SBarry Smith PetscErrorCode ierr; 2572b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2573a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2574b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2575b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2576b5b1a830SBarry Smith 2577b5b1a830SBarry Smith PetscFunctionBegin; 2578b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2579b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2580b5b1a830SBarry Smith 2581b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2582b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2583b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2584b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2585b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2586b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2587b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2588b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2589b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2590b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2591b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2592b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2593b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2594b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2595b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2596b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2597b5b1a830SBarry Smith PetscFunctionReturn(0); 2598b5b1a830SBarry Smith } 2599b5b1a830SBarry Smith 2600b5b1a830SBarry Smith 2601b5b1a830SBarry Smith #undef __FUNCT__ 2602b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2603b5b1a830SBarry Smith /* 2604b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2605b5b1a830SBarry Smith 2606b5b1a830SBarry Smith Level: developer 2607b5b1a830SBarry Smith 2608b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2609b5b1a830SBarry Smith 2610b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2611b5b1a830SBarry Smith */ 26127087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2613b5b1a830SBarry Smith { 2614b5b1a830SBarry Smith PetscErrorCode ierr; 2615b5b1a830SBarry Smith TSMatlabContext *sctx; 2616b5b1a830SBarry Smith 2617b5b1a830SBarry Smith PetscFunctionBegin; 2618b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2619b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2620b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2621b5b1a830SBarry Smith /* 2622b5b1a830SBarry Smith This should work, but it doesn't 2623b5b1a830SBarry Smith sctx->ctx = ctx; 2624b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2625b5b1a830SBarry Smith */ 2626b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2627b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2628b5b1a830SBarry Smith PetscFunctionReturn(0); 2629b5b1a830SBarry Smith } 2630b5b1a830SBarry Smith 2631325fc9f4SBarry Smith #endif 2632