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; 76a34d58ebSBarry Smith PetscViewerASCIIMonitor monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78bdad233fSMatthew Knepley 79bdad233fSMatthew Knepley PetscFunctionBegin; 800700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 817adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr); 82bdad233fSMatthew Knepley 83bdad233fSMatthew Knepley /* Handle generic TS options */ 84bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 85bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 86bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr); 87bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr); 88a7cc72afSBarry Smith if (opt) { 89bdad233fSMatthew Knepley ts->initial_time_step = ts->time_step = dt; 90bdad233fSMatthew Knepley } 91bdad233fSMatthew Knepley 92bdad233fSMatthew Knepley /* Monitor options */ 93a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 94eabae89aSBarry Smith if (flg) { 95050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)ts)->comm,monfilename,((PetscObject)ts)->tablevel,&monviewer);CHKERRQ(ierr); 96a34d58ebSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 97bdad233fSMatthew Knepley } 9890d69ab7SBarry Smith opt = PETSC_FALSE; 99acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 100a7cc72afSBarry Smith if (opt) { 101a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 102bdad233fSMatthew Knepley } 10390d69ab7SBarry Smith opt = PETSC_FALSE; 104acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 105a7cc72afSBarry Smith if (opt) { 106a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 107bdad233fSMatthew Knepley } 108bdad233fSMatthew Knepley 109bdad233fSMatthew Knepley /* Handle TS type options */ 110bdad233fSMatthew Knepley ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 111bdad233fSMatthew Knepley 112bdad233fSMatthew Knepley /* Handle specific TS options */ 113abc0a331SBarry Smith if (ts->ops->setfromoptions) { 114bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 115bdad233fSMatthew Knepley } 1165d973c19SBarry Smith 1175d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1185d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 119bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 120bdad233fSMatthew Knepley 121bdad233fSMatthew Knepley /* Handle subobject options */ 122bdad233fSMatthew Knepley switch(ts->problem_type) { 123156fc9a6SMatthew Knepley /* Should check for implicit/explicit */ 124bdad233fSMatthew Knepley case TS_LINEAR: 125abc0a331SBarry Smith if (ts->ksp) { 1268beb423aSHong Zhang ierr = KSPSetOperators(ts->ksp,ts->Arhs,ts->B,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 12794b7f48cSBarry Smith ierr = KSPSetFromOptions(ts->ksp);CHKERRQ(ierr); 128156fc9a6SMatthew Knepley } 129bdad233fSMatthew Knepley break; 130bdad233fSMatthew Knepley case TS_NONLINEAR: 131abc0a331SBarry Smith if (ts->snes) { 1327c236d22SBarry Smith /* this is a bit of a hack, but it gets the matrix information into SNES earlier 1337c236d22SBarry Smith so that SNES and KSP have more information to pick reasonable defaults 1347c0b301bSJed Brown before they allow users to set options 1357c0b301bSJed Brown * If ts->A has been set at this point, we are probably using the implicit form 1367c0b301bSJed Brown and Arhs will never be used. */ 1377c0b301bSJed Brown ierr = SNESSetJacobian(ts->snes,ts->A?ts->A:ts->Arhs,ts->B,0,ts);CHKERRQ(ierr); 138bdad233fSMatthew Knepley ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 139156fc9a6SMatthew Knepley } 140bdad233fSMatthew Knepley break; 141bdad233fSMatthew Knepley default: 142e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid problem type: %d", (int)ts->problem_type); 143bdad233fSMatthew Knepley } 144bdad233fSMatthew Knepley 145bdad233fSMatthew Knepley PetscFunctionReturn(0); 146bdad233fSMatthew Knepley } 147bdad233fSMatthew Knepley 148bdad233fSMatthew Knepley #undef __FUNCT__ 149bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions" 150bdad233fSMatthew Knepley /*@ 151bdad233fSMatthew Knepley TSViewFromOptions - This function visualizes the ts based upon user options. 152bdad233fSMatthew Knepley 153bdad233fSMatthew Knepley Collective on TS 154bdad233fSMatthew Knepley 155bdad233fSMatthew Knepley Input Parameter: 156bdad233fSMatthew Knepley . ts - The ts 157bdad233fSMatthew Knepley 158bdad233fSMatthew Knepley Level: intermediate 159bdad233fSMatthew Knepley 160bdad233fSMatthew Knepley .keywords: TS, view, options, database 161bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView() 162bdad233fSMatthew Knepley @*/ 1637087cfbeSBarry Smith PetscErrorCode TSViewFromOptions(TS ts,const char title[]) 164bdad233fSMatthew Knepley { 165bdad233fSMatthew Knepley PetscViewer viewer; 166bdad233fSMatthew Knepley PetscDraw draw; 167ace3abfcSBarry Smith PetscBool opt = PETSC_FALSE; 168e10c49a3SBarry Smith char fileName[PETSC_MAX_PATH_LEN]; 169dfbe8321SBarry Smith PetscErrorCode ierr; 170bdad233fSMatthew Knepley 171bdad233fSMatthew Knepley PetscFunctionBegin; 1727adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr); 173eabae89aSBarry Smith if (opt && !PetscPreLoadingOn) { 1747adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr); 175bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 176bdad233fSMatthew Knepley ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 177bdad233fSMatthew Knepley } 1788e83347fSKai Germaschewski opt = PETSC_FALSE; 179acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr); 180a7cc72afSBarry Smith if (opt) { 1817adad957SLisandro Dalcin ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr); 182bdad233fSMatthew Knepley ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr); 183a7cc72afSBarry Smith if (title) { 1841836bdbcSSatish Balay ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr); 185bdad233fSMatthew Knepley } else { 186bdad233fSMatthew Knepley ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr); 1877adad957SLisandro Dalcin ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr); 188bdad233fSMatthew Knepley } 189bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 190bdad233fSMatthew Knepley ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 191bdad233fSMatthew Knepley ierr = PetscDrawPause(draw);CHKERRQ(ierr); 192bdad233fSMatthew Knepley ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 193bdad233fSMatthew Knepley } 194bdad233fSMatthew Knepley PetscFunctionReturn(0); 195bdad233fSMatthew Knepley } 196bdad233fSMatthew Knepley 197bdad233fSMatthew Knepley #undef __FUNCT__ 1984a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 199a7a1495cSBarry Smith /*@ 2008c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 201a7a1495cSBarry Smith set with TSSetRHSJacobian(). 202a7a1495cSBarry Smith 203a7a1495cSBarry Smith Collective on TS and Vec 204a7a1495cSBarry Smith 205a7a1495cSBarry Smith Input Parameters: 206316643e7SJed Brown + ts - the TS context 207a7a1495cSBarry Smith . t - current timestep 208a7a1495cSBarry Smith - x - input vector 209a7a1495cSBarry Smith 210a7a1495cSBarry Smith Output Parameters: 211a7a1495cSBarry Smith + A - Jacobian matrix 212a7a1495cSBarry Smith . B - optional preconditioning matrix 213a7a1495cSBarry Smith - flag - flag indicating matrix structure 214a7a1495cSBarry Smith 215a7a1495cSBarry Smith Notes: 216a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 217a7a1495cSBarry Smith is used internally within the nonlinear solvers. 218a7a1495cSBarry Smith 21994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 220a7a1495cSBarry Smith flag parameter. 221a7a1495cSBarry Smith 222a7a1495cSBarry Smith TSComputeJacobian() is valid only for TS_NONLINEAR 223a7a1495cSBarry Smith 224a7a1495cSBarry Smith Level: developer 225a7a1495cSBarry Smith 226a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 227a7a1495cSBarry Smith 22894b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 229a7a1495cSBarry Smith @*/ 2307087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 231a7a1495cSBarry Smith { 232dfbe8321SBarry Smith PetscErrorCode ierr; 233a7a1495cSBarry Smith 234a7a1495cSBarry Smith PetscFunctionBegin; 2350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2360700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 237c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 23817186662SBarry Smith if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only"); 239000e7ae3SMatthew Knepley if (ts->ops->rhsjacobian) { 240d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 241a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 242a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 243000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 244a7a1495cSBarry Smith PetscStackPop; 245d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 246a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2470700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2480700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 249ef66eb69SBarry Smith } else { 250ef66eb69SBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 251ef66eb69SBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 252ef66eb69SBarry Smith if (*A != *B) { 253ef66eb69SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 254ef66eb69SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 255ef66eb69SBarry Smith } 256ef66eb69SBarry Smith } 257a7a1495cSBarry Smith PetscFunctionReturn(0); 258a7a1495cSBarry Smith } 259a7a1495cSBarry Smith 2604a2ae208SSatish Balay #undef __FUNCT__ 2614a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 262316643e7SJed Brown /*@ 263d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 264d763cef2SBarry Smith 265316643e7SJed Brown Collective on TS and Vec 266316643e7SJed Brown 267316643e7SJed Brown Input Parameters: 268316643e7SJed Brown + ts - the TS context 269316643e7SJed Brown . t - current time 270316643e7SJed Brown - x - state vector 271316643e7SJed Brown 272316643e7SJed Brown Output Parameter: 273316643e7SJed Brown . y - right hand side 274316643e7SJed Brown 275316643e7SJed Brown Note: 276316643e7SJed Brown Most users should not need to explicitly call this routine, as it 277316643e7SJed Brown is used internally within the nonlinear solvers. 278316643e7SJed Brown 279316643e7SJed Brown If the user did not provide a function but merely a matrix, 280d763cef2SBarry Smith this routine applies the matrix. 281316643e7SJed Brown 282316643e7SJed Brown Level: developer 283316643e7SJed Brown 284316643e7SJed Brown .keywords: TS, compute 285316643e7SJed Brown 286316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 287316643e7SJed Brown @*/ 288dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 289d763cef2SBarry Smith { 290dfbe8321SBarry Smith PetscErrorCode ierr; 291d763cef2SBarry Smith 292d763cef2SBarry Smith PetscFunctionBegin; 2930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2940700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2950700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 296d763cef2SBarry Smith 297d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 298000e7ae3SMatthew Knepley if (ts->ops->rhsfunction) { 299d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 300000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 301d763cef2SBarry Smith PetscStackPop; 302d16dab79SJed Brown } else if (ts->Arhs) { 303000e7ae3SMatthew Knepley if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */ 304d763cef2SBarry Smith MatStructure flg; 305d763cef2SBarry Smith PetscStackPush("TS user right-hand-side matrix function"); 3068beb423aSHong Zhang ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->Arhs,&ts->B,&flg,ts->jacP);CHKERRQ(ierr); 307d763cef2SBarry Smith PetscStackPop; 308d763cef2SBarry Smith } 3098beb423aSHong Zhang ierr = MatMult(ts->Arhs,x,y);CHKERRQ(ierr); 310d16dab79SJed Brown } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()"); 311d763cef2SBarry Smith 312d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 313d763cef2SBarry Smith 314d763cef2SBarry Smith PetscFunctionReturn(0); 315d763cef2SBarry Smith } 316d763cef2SBarry Smith 3174a2ae208SSatish Balay #undef __FUNCT__ 318316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 319316643e7SJed Brown /*@ 320316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 321316643e7SJed Brown 322316643e7SJed Brown Collective on TS and Vec 323316643e7SJed Brown 324316643e7SJed Brown Input Parameters: 325316643e7SJed Brown + ts - the TS context 326316643e7SJed Brown . t - current time 327316643e7SJed Brown . X - state vector 328316643e7SJed Brown - Xdot - time derivative of state vector 329316643e7SJed Brown 330316643e7SJed Brown Output Parameter: 331316643e7SJed Brown . Y - right hand side 332316643e7SJed Brown 333316643e7SJed Brown Note: 334316643e7SJed Brown Most users should not need to explicitly call this routine, as it 335316643e7SJed Brown is used internally within the nonlinear solvers. 336316643e7SJed Brown 337316643e7SJed Brown If the user did did not write their equations in implicit form, this 338316643e7SJed Brown function recasts them in implicit form. 339316643e7SJed Brown 340316643e7SJed Brown Level: developer 341316643e7SJed Brown 342316643e7SJed Brown .keywords: TS, compute 343316643e7SJed Brown 344316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 345316643e7SJed Brown @*/ 346316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y) 347316643e7SJed Brown { 348316643e7SJed Brown PetscErrorCode ierr; 349316643e7SJed Brown 350316643e7SJed Brown PetscFunctionBegin; 3510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3520700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3530700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3540700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 355316643e7SJed Brown 356316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 357316643e7SJed Brown if (ts->ops->ifunction) { 358316643e7SJed Brown PetscStackPush("TS user implicit function"); 359316643e7SJed Brown ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 360316643e7SJed Brown PetscStackPop; 361316643e7SJed Brown } else { 362316643e7SJed Brown if (ts->ops->rhsfunction) { 363316643e7SJed Brown PetscStackPush("TS user right-hand-side function"); 364316643e7SJed Brown ierr = (*ts->ops->rhsfunction)(ts,t,X,Y,ts->funP);CHKERRQ(ierr); 365316643e7SJed Brown PetscStackPop; 366316643e7SJed Brown } else { 367316643e7SJed Brown if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */ 368316643e7SJed Brown MatStructure flg; 3694a6899ffSJed Brown /* Note: flg is not being used. 3704a6899ffSJed Brown For it to be useful, we'd have to cache it and then apply it in TSComputeIJacobian. 3714a6899ffSJed Brown */ 372316643e7SJed Brown PetscStackPush("TS user right-hand-side matrix function"); 373316643e7SJed Brown ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->Arhs,&ts->B,&flg,ts->jacP);CHKERRQ(ierr); 374316643e7SJed Brown PetscStackPop; 375316643e7SJed Brown } 376316643e7SJed Brown ierr = MatMult(ts->Arhs,X,Y);CHKERRQ(ierr); 377316643e7SJed Brown } 378316643e7SJed Brown 3794a6899ffSJed Brown /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */ 3804a6899ffSJed Brown if (ts->Alhs) { 3814a6899ffSJed Brown if (ts->ops->lhsmatrix) { 3824a6899ffSJed Brown MatStructure flg; 3834a6899ffSJed Brown PetscStackPush("TS user left-hand-side matrix function"); 3844a6899ffSJed Brown ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,PETSC_NULL,&flg,ts->jacP);CHKERRQ(ierr); 3854a6899ffSJed Brown PetscStackPop; 3864a6899ffSJed Brown } 3874a6899ffSJed Brown ierr = VecScale(Y,-1.);CHKERRQ(ierr); 3884a6899ffSJed Brown ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr); 3894a6899ffSJed Brown } else { 390ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 391316643e7SJed Brown } 3924a6899ffSJed Brown } 393316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 394316643e7SJed Brown PetscFunctionReturn(0); 395316643e7SJed Brown } 396316643e7SJed Brown 397316643e7SJed Brown #undef __FUNCT__ 398316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 399316643e7SJed Brown /*@ 400316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 401316643e7SJed Brown 402316643e7SJed Brown Collective on TS and Vec 403316643e7SJed Brown 404316643e7SJed Brown Input 405316643e7SJed Brown Input Parameters: 406316643e7SJed Brown + ts - the TS context 407316643e7SJed Brown . t - current timestep 408316643e7SJed Brown . X - state vector 409316643e7SJed Brown . Xdot - time derivative of state vector 410316643e7SJed Brown - shift - shift to apply, see note below 411316643e7SJed Brown 412316643e7SJed Brown Output Parameters: 413316643e7SJed Brown + A - Jacobian matrix 414316643e7SJed Brown . B - optional preconditioning matrix 415316643e7SJed Brown - flag - flag indicating matrix structure 416316643e7SJed Brown 417316643e7SJed Brown Notes: 418316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 419316643e7SJed Brown 420316643e7SJed Brown dF/dX + shift*dF/dXdot 421316643e7SJed Brown 422316643e7SJed Brown Most users should not need to explicitly call this routine, as it 423316643e7SJed Brown is used internally within the nonlinear solvers. 424316643e7SJed Brown 425316643e7SJed Brown TSComputeIJacobian() is valid only for TS_NONLINEAR 426316643e7SJed Brown 427316643e7SJed Brown Level: developer 428316643e7SJed Brown 429316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 430316643e7SJed Brown 431316643e7SJed Brown .seealso: TSSetIJacobian() 43263495f91SJed Brown @*/ 4337087cfbeSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg) 434316643e7SJed Brown { 435316643e7SJed Brown PetscErrorCode ierr; 436316643e7SJed Brown 437316643e7SJed Brown PetscFunctionBegin; 4380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4390700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4400700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 441316643e7SJed Brown PetscValidPointer(A,6); 4420700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 443316643e7SJed Brown PetscValidPointer(B,7); 4440700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 445316643e7SJed Brown PetscValidPointer(flg,8); 446316643e7SJed Brown 4474a6899ffSJed Brown *flg = SAME_NONZERO_PATTERN; /* In case it we're solving a linear problem in which case it wouldn't get initialized below. */ 448316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 449316643e7SJed Brown if (ts->ops->ijacobian) { 450316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 451316643e7SJed Brown ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 452316643e7SJed Brown PetscStackPop; 453316643e7SJed Brown } else { 454316643e7SJed Brown if (ts->ops->rhsjacobian) { 455316643e7SJed Brown PetscStackPush("TS user right-hand-side Jacobian"); 456316643e7SJed Brown ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 457316643e7SJed Brown PetscStackPop; 458316643e7SJed Brown } else { 459316643e7SJed Brown ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 460316643e7SJed Brown ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 461316643e7SJed Brown if (*A != *B) { 462316643e7SJed Brown ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 463316643e7SJed Brown ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 464316643e7SJed Brown } 465316643e7SJed Brown } 466316643e7SJed Brown 467316643e7SJed Brown /* Convert to implicit form */ 468316643e7SJed Brown /* inefficient because these operations will normally traverse all matrix elements twice */ 469316643e7SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 4704a6899ffSJed Brown if (ts->Alhs) { 4714a6899ffSJed Brown ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 4724a6899ffSJed Brown } else { 473316643e7SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 4744a6899ffSJed Brown } 475316643e7SJed Brown if (*A != *B) { 476316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 477316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 478316643e7SJed Brown } 479316643e7SJed Brown } 480316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 481316643e7SJed Brown PetscFunctionReturn(0); 482316643e7SJed Brown } 483316643e7SJed Brown 484316643e7SJed Brown #undef __FUNCT__ 4854a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 486d763cef2SBarry Smith /*@C 487d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 488d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 489d763cef2SBarry Smith 4903f9fe445SBarry Smith Logically Collective on TS 491d763cef2SBarry Smith 492d763cef2SBarry Smith Input Parameters: 493d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 494d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 495d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 496d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 497d763cef2SBarry Smith 498d763cef2SBarry Smith Calling sequence of func: 49987828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 500d763cef2SBarry Smith 501d763cef2SBarry Smith + t - current timestep 502d763cef2SBarry Smith . u - input vector 503d763cef2SBarry Smith . F - function vector 504d763cef2SBarry Smith - ctx - [optional] user-defined function context 505d763cef2SBarry Smith 506d763cef2SBarry Smith Important: 50776f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 508d763cef2SBarry Smith 509d763cef2SBarry Smith Level: beginner 510d763cef2SBarry Smith 511d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 512d763cef2SBarry Smith 51376f2fa84SHong Zhang .seealso: TSSetMatrices() 514d763cef2SBarry Smith @*/ 5157087cfbeSBarry Smith PetscErrorCode TSSetRHSFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 516d763cef2SBarry Smith { 517d763cef2SBarry Smith PetscFunctionBegin; 518d763cef2SBarry Smith 5190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52017186662SBarry Smith if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem"); 521000e7ae3SMatthew Knepley ts->ops->rhsfunction = f; 522d763cef2SBarry Smith ts->funP = ctx; 523d763cef2SBarry Smith PetscFunctionReturn(0); 524d763cef2SBarry Smith } 525d763cef2SBarry Smith 5264a2ae208SSatish Balay #undef __FUNCT__ 52795f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices" 52895f0b562SHong Zhang /*@C 52995f0b562SHong Zhang TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs, 53095f0b562SHong Zhang where Alhs(t) U_t = Arhs(t) U. 53195f0b562SHong Zhang 5323f9fe445SBarry Smith Logically Collective on TS 53395f0b562SHong Zhang 53495f0b562SHong Zhang Input Parameters: 53595f0b562SHong Zhang + ts - the TS context obtained from TSCreate() 53695f0b562SHong Zhang . Arhs - matrix 53795f0b562SHong Zhang . frhs - the matrix evaluation routine for Arhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 53895f0b562SHong Zhang if Arhs is not a function of t. 53995f0b562SHong Zhang . Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix. 54095f0b562SHong Zhang . flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 54195f0b562SHong Zhang if Alhs is not a function of t. 54295f0b562SHong Zhang . flag - flag indicating information about the matrix structure of Arhs and Alhs. 54395f0b562SHong Zhang The available options are 54495f0b562SHong Zhang SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs 54595f0b562SHong Zhang DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs 54695f0b562SHong Zhang - ctx - [optional] user-defined context for private data for the 54795f0b562SHong Zhang matrix evaluation routine (may be PETSC_NULL) 54895f0b562SHong Zhang 54995f0b562SHong Zhang Calling sequence of func: 55095f0b562SHong Zhang $ func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx); 55195f0b562SHong Zhang 55295f0b562SHong Zhang + t - current timestep 55395f0b562SHong Zhang . A - matrix A, where U_t = A(t) U 55495f0b562SHong Zhang . B - preconditioner matrix, usually the same as A 55595f0b562SHong Zhang . flag - flag indicating information about the preconditioner matrix 55695f0b562SHong Zhang structure (same as flag in KSPSetOperators()) 55795f0b562SHong Zhang - ctx - [optional] user-defined context for matrix evaluation routine 55895f0b562SHong Zhang 55995f0b562SHong Zhang Notes: 56095f0b562SHong Zhang The routine func() takes Mat* as the matrix arguments rather than Mat. 56195f0b562SHong Zhang This allows the matrix evaluation routine to replace Arhs or Alhs with a 56295f0b562SHong Zhang completely new new matrix structure (not just different matrix elements) 56395f0b562SHong Zhang when appropriate, for instance, if the nonzero structure is changing 56495f0b562SHong Zhang throughout the global iterations. 56595f0b562SHong Zhang 56695f0b562SHong Zhang Important: 56795f0b562SHong Zhang The user MUST call either this routine or TSSetRHSFunction(). 56895f0b562SHong Zhang 56995f0b562SHong Zhang Level: beginner 57095f0b562SHong Zhang 57195f0b562SHong Zhang .keywords: TS, timestep, set, matrix 57295f0b562SHong Zhang 57395f0b562SHong Zhang .seealso: TSSetRHSFunction() 57495f0b562SHong Zhang @*/ 5757087cfbeSBarry Smith PetscErrorCode TSSetMatrices(TS ts,Mat Arhs,PetscErrorCode (*frhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),Mat Alhs,PetscErrorCode (*flhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),MatStructure flag,void *ctx) 57695f0b562SHong Zhang { 577*277b19d0SLisandro Dalcin PetscErrorCode ierr; 578*277b19d0SLisandro Dalcin 57995f0b562SHong Zhang PetscFunctionBegin; 5800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 581*277b19d0SLisandro Dalcin if (frhs) ts->ops->rhsmatrix = frhs; 582*277b19d0SLisandro Dalcin if (flhs) ts->ops->lhsmatrix = flhs; 583*277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 58492af4f6aSHong Zhang if (Arhs){ 5850700a824SBarry Smith PetscValidHeaderSpecific(Arhs,MAT_CLASSID,2); 58695f0b562SHong Zhang PetscCheckSameComm(ts,1,Arhs,2); 587*277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)Arhs);CHKERRQ(ierr); 588*277b19d0SLisandro Dalcin if (ts->Arhs) {ierr = MatDestroy(ts->Arhs);CHKERRQ(ierr);} 58995f0b562SHong Zhang ts->Arhs = Arhs; 59092af4f6aSHong Zhang } 59192af4f6aSHong Zhang if (Alhs){ 5920700a824SBarry Smith PetscValidHeaderSpecific(Alhs,MAT_CLASSID,4); 59392af4f6aSHong Zhang PetscCheckSameComm(ts,1,Alhs,4); 594*277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)Alhs);CHKERRQ(ierr); 595*277b19d0SLisandro Dalcin if (ts->Alhs) {ierr = MatDestroy(ts->Alhs);CHKERRQ(ierr);} 59695f0b562SHong Zhang ts->Alhs = Alhs; 59792af4f6aSHong Zhang } 59895f0b562SHong Zhang ts->matflg = flag; 59995f0b562SHong Zhang PetscFunctionReturn(0); 60095f0b562SHong Zhang } 601d763cef2SBarry Smith 602aa644b49SHong Zhang #undef __FUNCT__ 603cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices" 604cda39b92SHong Zhang /*@C 605cda39b92SHong Zhang TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep, 606cda39b92SHong Zhang where Alhs(t) U_t = Arhs(t) U. 607cda39b92SHong Zhang 608cda39b92SHong Zhang Not Collective, but parallel objects are returned if TS is parallel 609cda39b92SHong Zhang 610cda39b92SHong Zhang Input Parameter: 611cda39b92SHong Zhang . ts - The TS context obtained from TSCreate() 612cda39b92SHong Zhang 613cda39b92SHong Zhang Output Parameters: 614cda39b92SHong Zhang + Arhs - The right-hand side matrix 615cda39b92SHong Zhang . Alhs - The left-hand side matrix 616cda39b92SHong Zhang - ctx - User-defined context for matrix evaluation routine 617cda39b92SHong Zhang 618cda39b92SHong Zhang Notes: You can pass in PETSC_NULL for any return argument you do not need. 619cda39b92SHong Zhang 620cda39b92SHong Zhang Level: intermediate 621cda39b92SHong Zhang 622cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian() 623cda39b92SHong Zhang 624cda39b92SHong Zhang .keywords: TS, timestep, get, matrix 625cda39b92SHong Zhang 626cda39b92SHong Zhang @*/ 6277087cfbeSBarry Smith PetscErrorCode TSGetMatrices(TS ts,Mat *Arhs,Mat *Alhs,void **ctx) 628cda39b92SHong Zhang { 629cda39b92SHong Zhang PetscFunctionBegin; 6300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 631cda39b92SHong Zhang if (Arhs) *Arhs = ts->Arhs; 632cda39b92SHong Zhang if (Alhs) *Alhs = ts->Alhs; 633cda39b92SHong Zhang if (ctx) *ctx = ts->jacP; 634cda39b92SHong Zhang PetscFunctionReturn(0); 635cda39b92SHong Zhang } 636cda39b92SHong Zhang 637cda39b92SHong Zhang #undef __FUNCT__ 6384a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 639d763cef2SBarry Smith /*@C 640d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 641d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 64276f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 643d763cef2SBarry Smith 6443f9fe445SBarry Smith Logically Collective on TS 645d763cef2SBarry Smith 646d763cef2SBarry Smith Input Parameters: 647d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 648d763cef2SBarry Smith . A - Jacobian matrix 649d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 650d763cef2SBarry Smith . f - the Jacobian evaluation routine 651d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 652d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 653d763cef2SBarry Smith 654d763cef2SBarry Smith Calling sequence of func: 65587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 656d763cef2SBarry Smith 657d763cef2SBarry Smith + t - current timestep 658d763cef2SBarry Smith . u - input vector 659d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 660d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 661d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 66294b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 663d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 664d763cef2SBarry Smith 665d763cef2SBarry Smith Notes: 66694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 667d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 668d763cef2SBarry Smith 669d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 670d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 67156335db2SHong Zhang completely new matrix structure (not just different matrix elements) 672d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 673d763cef2SBarry Smith throughout the global iterations. 674d763cef2SBarry Smith 675d763cef2SBarry Smith Level: beginner 676d763cef2SBarry Smith 677d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 678d763cef2SBarry Smith 679d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 68076f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 681d763cef2SBarry Smith 682d763cef2SBarry Smith @*/ 6837087cfbeSBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,PetscErrorCode (*f)(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 684d763cef2SBarry Smith { 685*277b19d0SLisandro Dalcin PetscErrorCode ierr; 686*277b19d0SLisandro Dalcin 687d763cef2SBarry Smith PetscFunctionBegin; 6880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 689*277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 690*277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 691*277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 692*277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 69317186662SBarry Smith if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()"); 694d763cef2SBarry Smith 695*277b19d0SLisandro Dalcin if (f) ts->ops->rhsjacobian = f; 696*277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 697*277b19d0SLisandro Dalcin if (A) { 698*277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 699*277b19d0SLisandro Dalcin if (ts->Arhs) {ierr = MatDestroy(ts->Arhs);CHKERRQ(ierr);} 7008beb423aSHong Zhang ts->Arhs = A; 701*277b19d0SLisandro Dalcin } 702*277b19d0SLisandro Dalcin if (B) { 703*277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 704*277b19d0SLisandro Dalcin if (ts->B) {ierr = MatDestroy(ts->B);CHKERRQ(ierr);} 705d763cef2SBarry Smith ts->B = B; 706*277b19d0SLisandro Dalcin } 707d763cef2SBarry Smith PetscFunctionReturn(0); 708d763cef2SBarry Smith } 709d763cef2SBarry Smith 710316643e7SJed Brown 711316643e7SJed Brown #undef __FUNCT__ 712316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 713316643e7SJed Brown /*@C 714316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 715316643e7SJed Brown 7163f9fe445SBarry Smith Logically Collective on TS 717316643e7SJed Brown 718316643e7SJed Brown Input Parameters: 719316643e7SJed Brown + ts - the TS context obtained from TSCreate() 720316643e7SJed Brown . f - the function evaluation routine 721316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 722316643e7SJed Brown 723316643e7SJed Brown Calling sequence of f: 724316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 725316643e7SJed Brown 726316643e7SJed Brown + t - time at step/stage being solved 727316643e7SJed Brown . u - state vector 728316643e7SJed Brown . u_t - time derivative of state vector 729316643e7SJed Brown . F - function vector 730316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 731316643e7SJed Brown 732316643e7SJed Brown Important: 733316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 734316643e7SJed Brown 735316643e7SJed Brown Level: beginner 736316643e7SJed Brown 737316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 738316643e7SJed Brown 739316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 740316643e7SJed Brown @*/ 7417087cfbeSBarry Smith PetscErrorCode TSSetIFunction(TS ts,TSIFunction f,void *ctx) 742316643e7SJed Brown { 743316643e7SJed Brown 744316643e7SJed Brown PetscFunctionBegin; 7450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 746316643e7SJed Brown ts->ops->ifunction = f; 747316643e7SJed Brown ts->funP = ctx; 748316643e7SJed Brown PetscFunctionReturn(0); 749316643e7SJed Brown } 750316643e7SJed Brown 751316643e7SJed Brown #undef __FUNCT__ 752316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 753316643e7SJed Brown /*@C 7541b4a444bSJed Brown TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t which is 7551b4a444bSJed Brown the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 7561b4a444bSJed Brown The time integrator internally approximates U_t by W+a*U where the positive "shift" 7571b4a444bSJed Brown a and vector W depend on the integration method, step size, and past states. 758316643e7SJed Brown 7593f9fe445SBarry Smith Logically Collective on TS 760316643e7SJed Brown 761316643e7SJed Brown Input Parameters: 762316643e7SJed Brown + ts - the TS context obtained from TSCreate() 763316643e7SJed Brown . A - Jacobian matrix 764316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 765316643e7SJed Brown . f - the Jacobian evaluation routine 766316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 767316643e7SJed Brown 768316643e7SJed Brown Calling sequence of f: 7691b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 770316643e7SJed Brown 771316643e7SJed Brown + t - time at step/stage being solved 7721b4a444bSJed Brown . U - state vector 7731b4a444bSJed Brown . U_t - time derivative of state vector 774316643e7SJed Brown . a - shift 7751b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 776316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 777316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 778316643e7SJed Brown structure (same as flag in KSPSetOperators()) 779316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 780316643e7SJed Brown 781316643e7SJed Brown Notes: 782316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 783316643e7SJed Brown 784316643e7SJed Brown Level: beginner 785316643e7SJed Brown 786316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 787316643e7SJed Brown 788316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 789316643e7SJed Brown 790316643e7SJed Brown @*/ 7917087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 792316643e7SJed Brown { 793316643e7SJed Brown PetscErrorCode ierr; 794316643e7SJed Brown 795316643e7SJed Brown PetscFunctionBegin; 7960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7970700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 7980700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 799316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 800316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 801316643e7SJed Brown if (f) ts->ops->ijacobian = f; 802316643e7SJed Brown if (ctx) ts->jacP = ctx; 803316643e7SJed Brown if (A) { 804316643e7SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 805316643e7SJed Brown if (ts->A) {ierr = MatDestroy(ts->A);CHKERRQ(ierr);} 806316643e7SJed Brown ts->A = A; 807316643e7SJed Brown } 808316643e7SJed Brown if (B) { 809316643e7SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 810316643e7SJed Brown if (ts->B) {ierr = MatDestroy(ts->B);CHKERRQ(ierr);} 811316643e7SJed Brown ts->B = B; 812316643e7SJed Brown } 813316643e7SJed Brown PetscFunctionReturn(0); 814316643e7SJed Brown } 815316643e7SJed Brown 8164a2ae208SSatish Balay #undef __FUNCT__ 8174a2ae208SSatish Balay #define __FUNCT__ "TSView" 8187e2c5f70SBarry Smith /*@C 819d763cef2SBarry Smith TSView - Prints the TS data structure. 820d763cef2SBarry Smith 8214c49b128SBarry Smith Collective on TS 822d763cef2SBarry Smith 823d763cef2SBarry Smith Input Parameters: 824d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 825d763cef2SBarry Smith - viewer - visualization context 826d763cef2SBarry Smith 827d763cef2SBarry Smith Options Database Key: 828d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 829d763cef2SBarry Smith 830d763cef2SBarry Smith Notes: 831d763cef2SBarry Smith The available visualization contexts include 832b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 833b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 834d763cef2SBarry Smith output where only the first processor opens 835d763cef2SBarry Smith the file. All other processors send their 836d763cef2SBarry Smith data to the first processor to print. 837d763cef2SBarry Smith 838d763cef2SBarry Smith The user can open an alternative visualization context with 839b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 840d763cef2SBarry Smith 841d763cef2SBarry Smith Level: beginner 842d763cef2SBarry Smith 843d763cef2SBarry Smith .keywords: TS, timestep, view 844d763cef2SBarry Smith 845b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 846d763cef2SBarry Smith @*/ 8477087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 848d763cef2SBarry Smith { 849dfbe8321SBarry Smith PetscErrorCode ierr; 850a313700dSBarry Smith const TSType type; 851ace3abfcSBarry Smith PetscBool iascii,isstring; 852d763cef2SBarry Smith 853d763cef2SBarry Smith PetscFunctionBegin; 8540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8553050cee2SBarry Smith if (!viewer) { 8567adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8573050cee2SBarry Smith } 8580700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 859c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 860fd16b177SBarry Smith 8612692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8622692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 86332077d6dSBarry Smith if (iascii) { 864317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 865000e7ae3SMatthew Knepley if (ts->ops->view) { 866b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 867000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 868b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 869d763cef2SBarry Smith } 87077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 871a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 872d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 87377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 874d763cef2SBarry Smith } 87577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 8760f5bd95cSBarry Smith } else if (isstring) { 877a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 878b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 879d763cef2SBarry Smith } 880b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 88194b7f48cSBarry Smith if (ts->ksp) {ierr = KSPView(ts->ksp,viewer);CHKERRQ(ierr);} 882d763cef2SBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 883b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 884d763cef2SBarry Smith PetscFunctionReturn(0); 885d763cef2SBarry Smith } 886d763cef2SBarry Smith 887d763cef2SBarry Smith 8884a2ae208SSatish Balay #undef __FUNCT__ 8894a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 890d763cef2SBarry Smith /*@C 891d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 892d763cef2SBarry Smith the timesteppers. 893d763cef2SBarry Smith 8943f9fe445SBarry Smith Logically Collective on TS 895d763cef2SBarry Smith 896d763cef2SBarry Smith Input Parameters: 897d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 898d763cef2SBarry Smith - usrP - optional user context 899d763cef2SBarry Smith 900d763cef2SBarry Smith Level: intermediate 901d763cef2SBarry Smith 902d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 903d763cef2SBarry Smith 904d763cef2SBarry Smith .seealso: TSGetApplicationContext() 905d763cef2SBarry Smith @*/ 9067087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 907d763cef2SBarry Smith { 908d763cef2SBarry Smith PetscFunctionBegin; 9090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 910d763cef2SBarry Smith ts->user = usrP; 911d763cef2SBarry Smith PetscFunctionReturn(0); 912d763cef2SBarry Smith } 913d763cef2SBarry Smith 9144a2ae208SSatish Balay #undef __FUNCT__ 9154a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 916d763cef2SBarry Smith /*@C 917d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 918d763cef2SBarry Smith timestepper. 919d763cef2SBarry Smith 920d763cef2SBarry Smith Not Collective 921d763cef2SBarry Smith 922d763cef2SBarry Smith Input Parameter: 923d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 924d763cef2SBarry Smith 925d763cef2SBarry Smith Output Parameter: 926d763cef2SBarry Smith . usrP - user context 927d763cef2SBarry Smith 928d763cef2SBarry Smith Level: intermediate 929d763cef2SBarry Smith 930d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 931d763cef2SBarry Smith 932d763cef2SBarry Smith .seealso: TSSetApplicationContext() 933d763cef2SBarry Smith @*/ 9347087cfbeSBarry Smith PetscErrorCode TSGetApplicationContext(TS ts,void **usrP) 935d763cef2SBarry Smith { 936d763cef2SBarry Smith PetscFunctionBegin; 9370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 938d763cef2SBarry Smith *usrP = ts->user; 939d763cef2SBarry Smith PetscFunctionReturn(0); 940d763cef2SBarry Smith } 941d763cef2SBarry Smith 9424a2ae208SSatish Balay #undef __FUNCT__ 9434a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 944d763cef2SBarry Smith /*@ 945d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 946d763cef2SBarry Smith 947d763cef2SBarry Smith Not Collective 948d763cef2SBarry Smith 949d763cef2SBarry Smith Input Parameter: 950d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 951d763cef2SBarry Smith 952d763cef2SBarry Smith Output Parameter: 953d763cef2SBarry Smith . iter - number steps so far 954d763cef2SBarry Smith 955d763cef2SBarry Smith Level: intermediate 956d763cef2SBarry Smith 957d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 958d763cef2SBarry Smith @*/ 9597087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 960d763cef2SBarry Smith { 961d763cef2SBarry Smith PetscFunctionBegin; 9620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9634482741eSBarry Smith PetscValidIntPointer(iter,2); 964d763cef2SBarry Smith *iter = ts->steps; 965d763cef2SBarry Smith PetscFunctionReturn(0); 966d763cef2SBarry Smith } 967d763cef2SBarry Smith 9684a2ae208SSatish Balay #undef __FUNCT__ 9694a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 970d763cef2SBarry Smith /*@ 971d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 972d763cef2SBarry Smith as well as the initial time. 973d763cef2SBarry Smith 9743f9fe445SBarry Smith Logically Collective on TS 975d763cef2SBarry Smith 976d763cef2SBarry Smith Input Parameters: 977d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 978d763cef2SBarry Smith . initial_time - the initial time 979d763cef2SBarry Smith - time_step - the size of the timestep 980d763cef2SBarry Smith 981d763cef2SBarry Smith Level: intermediate 982d763cef2SBarry Smith 983d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 984d763cef2SBarry Smith 985d763cef2SBarry Smith .keywords: TS, set, initial, timestep 986d763cef2SBarry Smith @*/ 9877087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 988d763cef2SBarry Smith { 989d763cef2SBarry Smith PetscFunctionBegin; 9900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 991d763cef2SBarry Smith ts->time_step = time_step; 992d763cef2SBarry Smith ts->initial_time_step = time_step; 993d763cef2SBarry Smith ts->ptime = initial_time; 994d763cef2SBarry Smith PetscFunctionReturn(0); 995d763cef2SBarry Smith } 996d763cef2SBarry Smith 9974a2ae208SSatish Balay #undef __FUNCT__ 9984a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 999d763cef2SBarry Smith /*@ 1000d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1001d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1002d763cef2SBarry Smith 10033f9fe445SBarry Smith Logically Collective on TS 1004d763cef2SBarry Smith 1005d763cef2SBarry Smith Input Parameters: 1006d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1007d763cef2SBarry Smith - time_step - the size of the timestep 1008d763cef2SBarry Smith 1009d763cef2SBarry Smith Level: intermediate 1010d763cef2SBarry Smith 1011d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1012d763cef2SBarry Smith 1013d763cef2SBarry Smith .keywords: TS, set, timestep 1014d763cef2SBarry Smith @*/ 10157087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1016d763cef2SBarry Smith { 1017d763cef2SBarry Smith PetscFunctionBegin; 10180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1019c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1020d763cef2SBarry Smith ts->time_step = time_step; 1021d763cef2SBarry Smith PetscFunctionReturn(0); 1022d763cef2SBarry Smith } 1023d763cef2SBarry Smith 10244a2ae208SSatish Balay #undef __FUNCT__ 10254a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1026d763cef2SBarry Smith /*@ 1027d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1028d763cef2SBarry Smith 1029d763cef2SBarry Smith Not Collective 1030d763cef2SBarry Smith 1031d763cef2SBarry Smith Input Parameter: 1032d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1033d763cef2SBarry Smith 1034d763cef2SBarry Smith Output Parameter: 1035d763cef2SBarry Smith . dt - the current timestep size 1036d763cef2SBarry Smith 1037d763cef2SBarry Smith Level: intermediate 1038d763cef2SBarry Smith 1039d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1040d763cef2SBarry Smith 1041d763cef2SBarry Smith .keywords: TS, get, timestep 1042d763cef2SBarry Smith @*/ 10437087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1044d763cef2SBarry Smith { 1045d763cef2SBarry Smith PetscFunctionBegin; 10460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10474482741eSBarry Smith PetscValidDoublePointer(dt,2); 1048d763cef2SBarry Smith *dt = ts->time_step; 1049d763cef2SBarry Smith PetscFunctionReturn(0); 1050d763cef2SBarry Smith } 1051d763cef2SBarry Smith 10524a2ae208SSatish Balay #undef __FUNCT__ 10534a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1054d8e5e3e6SSatish Balay /*@ 1055d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1056d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1057d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1058d763cef2SBarry Smith the solution at the next timestep has been calculated. 1059d763cef2SBarry Smith 1060d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1061d763cef2SBarry Smith 1062d763cef2SBarry Smith Input Parameter: 1063d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1064d763cef2SBarry Smith 1065d763cef2SBarry Smith Output Parameter: 1066d763cef2SBarry Smith . v - the vector containing the solution 1067d763cef2SBarry Smith 1068d763cef2SBarry Smith Level: intermediate 1069d763cef2SBarry Smith 1070d763cef2SBarry Smith .seealso: TSGetTimeStep() 1071d763cef2SBarry Smith 1072d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1073d763cef2SBarry Smith @*/ 10747087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1075d763cef2SBarry Smith { 1076d763cef2SBarry Smith PetscFunctionBegin; 10770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10784482741eSBarry Smith PetscValidPointer(v,2); 10798737fe31SLisandro Dalcin *v = ts->vec_sol; 1080d763cef2SBarry Smith PetscFunctionReturn(0); 1081d763cef2SBarry Smith } 1082d763cef2SBarry Smith 1083bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 10844a2ae208SSatish Balay #undef __FUNCT__ 1085bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1086d8e5e3e6SSatish Balay /*@ 1087bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1088d763cef2SBarry Smith 1089bdad233fSMatthew Knepley Not collective 1090d763cef2SBarry Smith 1091bdad233fSMatthew Knepley Input Parameters: 1092bdad233fSMatthew Knepley + ts - The TS 1093bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1094d763cef2SBarry Smith .vb 1095d763cef2SBarry Smith U_t = A U 1096d763cef2SBarry Smith U_t = A(t) U 1097d763cef2SBarry Smith U_t = F(t,U) 1098d763cef2SBarry Smith .ve 1099d763cef2SBarry Smith 1100d763cef2SBarry Smith Level: beginner 1101d763cef2SBarry Smith 1102bdad233fSMatthew Knepley .keywords: TS, problem type 1103bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1104d763cef2SBarry Smith @*/ 11057087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1106a7cc72afSBarry Smith { 1107d763cef2SBarry Smith PetscFunctionBegin; 11080700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1109bdad233fSMatthew Knepley ts->problem_type = type; 1110d763cef2SBarry Smith PetscFunctionReturn(0); 1111d763cef2SBarry Smith } 1112d763cef2SBarry Smith 1113bdad233fSMatthew Knepley #undef __FUNCT__ 1114bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1115bdad233fSMatthew Knepley /*@C 1116bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1117bdad233fSMatthew Knepley 1118bdad233fSMatthew Knepley Not collective 1119bdad233fSMatthew Knepley 1120bdad233fSMatthew Knepley Input Parameter: 1121bdad233fSMatthew Knepley . ts - The TS 1122bdad233fSMatthew Knepley 1123bdad233fSMatthew Knepley Output Parameter: 1124bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1125bdad233fSMatthew Knepley .vb 1126bdad233fSMatthew Knepley U_t = A U 1127bdad233fSMatthew Knepley U_t = A(t) U 1128bdad233fSMatthew Knepley U_t = F(t,U) 1129bdad233fSMatthew Knepley .ve 1130bdad233fSMatthew Knepley 1131bdad233fSMatthew Knepley Level: beginner 1132bdad233fSMatthew Knepley 1133bdad233fSMatthew Knepley .keywords: TS, problem type 1134bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1135bdad233fSMatthew Knepley @*/ 11367087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1137a7cc72afSBarry Smith { 1138bdad233fSMatthew Knepley PetscFunctionBegin; 11390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11404482741eSBarry Smith PetscValidIntPointer(type,2); 1141bdad233fSMatthew Knepley *type = ts->problem_type; 1142bdad233fSMatthew Knepley PetscFunctionReturn(0); 1143bdad233fSMatthew Knepley } 1144d763cef2SBarry Smith 11454a2ae208SSatish Balay #undef __FUNCT__ 11464a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1147d763cef2SBarry Smith /*@ 1148d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1149d763cef2SBarry Smith of a timestepper. 1150d763cef2SBarry Smith 1151d763cef2SBarry Smith Collective on TS 1152d763cef2SBarry Smith 1153d763cef2SBarry Smith Input Parameter: 1154d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1155d763cef2SBarry Smith 1156d763cef2SBarry Smith Notes: 1157d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1158d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1159d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1160d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1161d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1162d763cef2SBarry Smith 1163d763cef2SBarry Smith Level: advanced 1164d763cef2SBarry Smith 1165d763cef2SBarry Smith .keywords: TS, timestep, setup 1166d763cef2SBarry Smith 1167d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1168d763cef2SBarry Smith @*/ 11697087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1170d763cef2SBarry Smith { 1171dfbe8321SBarry Smith PetscErrorCode ierr; 1172d763cef2SBarry Smith 1173d763cef2SBarry Smith PetscFunctionBegin; 11740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1175*277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1176*277b19d0SLisandro Dalcin 11777adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 11789596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1179d763cef2SBarry Smith } 1180*277b19d0SLisandro Dalcin 1181*277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1182*277b19d0SLisandro Dalcin 1183*277b19d0SLisandro Dalcin if (ts->ops->setup) { 1184000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1185*277b19d0SLisandro Dalcin } 1186*277b19d0SLisandro Dalcin 1187*277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1188*277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1189*277b19d0SLisandro Dalcin } 1190*277b19d0SLisandro Dalcin 1191*277b19d0SLisandro Dalcin #undef __FUNCT__ 1192*277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1193*277b19d0SLisandro Dalcin /*@ 1194*277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1195*277b19d0SLisandro Dalcin 1196*277b19d0SLisandro Dalcin Collective on TS 1197*277b19d0SLisandro Dalcin 1198*277b19d0SLisandro Dalcin Input Parameter: 1199*277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1200*277b19d0SLisandro Dalcin 1201*277b19d0SLisandro Dalcin Level: beginner 1202*277b19d0SLisandro Dalcin 1203*277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1204*277b19d0SLisandro Dalcin 1205*277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1206*277b19d0SLisandro Dalcin @*/ 1207*277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1208*277b19d0SLisandro Dalcin { 1209*277b19d0SLisandro Dalcin PetscErrorCode ierr; 1210*277b19d0SLisandro Dalcin 1211*277b19d0SLisandro Dalcin PetscFunctionBegin; 1212*277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1213*277b19d0SLisandro Dalcin if (ts->ops->reset) { 1214*277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1215*277b19d0SLisandro Dalcin } 1216*277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1217*277b19d0SLisandro Dalcin if (ts->ksp) {ierr = KSPReset(ts->ksp);CHKERRQ(ierr);} 1218*277b19d0SLisandro Dalcin if (ts->A) {ierr = MatDestroy(ts->A);CHKERRQ(ierr);} 1219*277b19d0SLisandro Dalcin if (ts->B) {ierr = MatDestroy(ts->B);CHKERRQ(ierr);} 1220*277b19d0SLisandro Dalcin if (ts->Arhs) {ierr = MatDestroy(ts->Arhs);CHKERRQ(ierr);} 1221*277b19d0SLisandro Dalcin if (ts->Alhs) {ierr = MatDestroy(ts->Alhs);CHKERRQ(ierr);} 1222*277b19d0SLisandro Dalcin if (ts->vec_sol) {ierr = VecDestroy(ts->vec_sol);CHKERRQ(ierr);} 1223*277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1224*277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1225d763cef2SBarry Smith PetscFunctionReturn(0); 1226d763cef2SBarry Smith } 1227d763cef2SBarry Smith 12284a2ae208SSatish Balay #undef __FUNCT__ 12294a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1230d8e5e3e6SSatish Balay /*@ 1231d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1232d763cef2SBarry Smith with TSCreate(). 1233d763cef2SBarry Smith 1234d763cef2SBarry Smith Collective on TS 1235d763cef2SBarry Smith 1236d763cef2SBarry Smith Input Parameter: 1237d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1238d763cef2SBarry Smith 1239d763cef2SBarry Smith Level: beginner 1240d763cef2SBarry Smith 1241d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1242d763cef2SBarry Smith 1243d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1244d763cef2SBarry Smith @*/ 12457087cfbeSBarry Smith PetscErrorCode TSDestroy(TS ts) 1246d763cef2SBarry Smith { 12476849ba73SBarry Smith PetscErrorCode ierr; 1248d763cef2SBarry Smith 1249d763cef2SBarry Smith PetscFunctionBegin; 12500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12517adad957SLisandro Dalcin if (--((PetscObject)ts)->refct > 0) PetscFunctionReturn(0); 1252d763cef2SBarry Smith 1253*277b19d0SLisandro Dalcin ierr = TSReset(ts);CHKERRQ(ierr); 1254*277b19d0SLisandro Dalcin 1255be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12560f5bd95cSBarry Smith ierr = PetscObjectDepublish(ts);CHKERRQ(ierr); 1257*277b19d0SLisandro Dalcin if (ts->ops->destroy) {ierr = (*ts->ops->destroy)(ts);CHKERRQ(ierr);} 12586d4c513bSLisandro Dalcin 1259d763cef2SBarry Smith if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);} 1260*277b19d0SLisandro Dalcin if (ts->ksp) {ierr = KSPDestroy(ts->ksp);CHKERRQ(ierr);} 1261*277b19d0SLisandro Dalcin if (ts->dm) {ierr = DMDestroy(ts->dm);CHKERRQ(ierr);} 1262a6570f20SBarry Smith ierr = TSMonitorCancel(ts);CHKERRQ(ierr); 12636d4c513bSLisandro Dalcin 1264a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1265d763cef2SBarry Smith PetscFunctionReturn(0); 1266d763cef2SBarry Smith } 1267d763cef2SBarry Smith 12684a2ae208SSatish Balay #undef __FUNCT__ 12694a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1270d8e5e3e6SSatish Balay /*@ 1271d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1272d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1273d763cef2SBarry Smith 1274d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1275d763cef2SBarry Smith 1276d763cef2SBarry Smith Input Parameter: 1277d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1278d763cef2SBarry Smith 1279d763cef2SBarry Smith Output Parameter: 1280d763cef2SBarry Smith . snes - the nonlinear solver context 1281d763cef2SBarry Smith 1282d763cef2SBarry Smith Notes: 1283d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1284d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 128594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1286d763cef2SBarry Smith 1287d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1288d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1289d763cef2SBarry Smith 1290d763cef2SBarry Smith Level: beginner 1291d763cef2SBarry Smith 1292d763cef2SBarry Smith .keywords: timestep, get, SNES 1293d763cef2SBarry Smith @*/ 12947087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1295d763cef2SBarry Smith { 1296d763cef2SBarry Smith PetscFunctionBegin; 12970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12984482741eSBarry Smith PetscValidPointer(snes,2); 129917186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"SNES is not created yet. Call TSSetType() first"); 1300e32f2f54SBarry Smith if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetKSP()"); 1301d763cef2SBarry Smith *snes = ts->snes; 1302d763cef2SBarry Smith PetscFunctionReturn(0); 1303d763cef2SBarry Smith } 1304d763cef2SBarry Smith 13054a2ae208SSatish Balay #undef __FUNCT__ 130694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1307d8e5e3e6SSatish Balay /*@ 130894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1309d763cef2SBarry Smith a TS (timestepper) context. 1310d763cef2SBarry Smith 131194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1312d763cef2SBarry Smith 1313d763cef2SBarry Smith Input Parameter: 1314d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1315d763cef2SBarry Smith 1316d763cef2SBarry Smith Output Parameter: 131794b7f48cSBarry Smith . ksp - the nonlinear solver context 1318d763cef2SBarry Smith 1319d763cef2SBarry Smith Notes: 132094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1321d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1322d763cef2SBarry Smith KSP and PC contexts as well. 1323d763cef2SBarry Smith 132494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 132594b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1326d763cef2SBarry Smith 1327d763cef2SBarry Smith Level: beginner 1328d763cef2SBarry Smith 132994b7f48cSBarry Smith .keywords: timestep, get, KSP 1330d763cef2SBarry Smith @*/ 13317087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1332d763cef2SBarry Smith { 1333d763cef2SBarry Smith PetscFunctionBegin; 13340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13354482741eSBarry Smith PetscValidPointer(ksp,2); 133617186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1337e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 133894b7f48cSBarry Smith *ksp = ts->ksp; 1339d763cef2SBarry Smith PetscFunctionReturn(0); 1340d763cef2SBarry Smith } 1341d763cef2SBarry Smith 1342d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1343d763cef2SBarry Smith 13444a2ae208SSatish Balay #undef __FUNCT__ 1345adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1346adb62b0dSMatthew Knepley /*@ 1347adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1348adb62b0dSMatthew Knepley maximum time for iteration. 1349adb62b0dSMatthew Knepley 13503f9fe445SBarry Smith Not Collective 1351adb62b0dSMatthew Knepley 1352adb62b0dSMatthew Knepley Input Parameters: 1353adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1354adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1355adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1356adb62b0dSMatthew Knepley 1357adb62b0dSMatthew Knepley Level: intermediate 1358adb62b0dSMatthew Knepley 1359adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1360adb62b0dSMatthew Knepley @*/ 13617087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1362adb62b0dSMatthew Knepley { 1363adb62b0dSMatthew Knepley PetscFunctionBegin; 13640700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1365abc0a331SBarry Smith if (maxsteps) { 13664482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1367adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1368adb62b0dSMatthew Knepley } 1369abc0a331SBarry Smith if (maxtime ) { 13704482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1371adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1372adb62b0dSMatthew Knepley } 1373adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1374adb62b0dSMatthew Knepley } 1375adb62b0dSMatthew Knepley 1376adb62b0dSMatthew Knepley #undef __FUNCT__ 13774a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1378d763cef2SBarry Smith /*@ 1379d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1380d763cef2SBarry Smith maximum time for iteration. 1381d763cef2SBarry Smith 13823f9fe445SBarry Smith Logically Collective on TS 1383d763cef2SBarry Smith 1384d763cef2SBarry Smith Input Parameters: 1385d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1386d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1387d763cef2SBarry Smith - maxtime - final time to iterate to 1388d763cef2SBarry Smith 1389d763cef2SBarry Smith Options Database Keys: 1390d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1391d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1392d763cef2SBarry Smith 1393d763cef2SBarry Smith Notes: 1394d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1395d763cef2SBarry Smith 1396d763cef2SBarry Smith Level: intermediate 1397d763cef2SBarry Smith 1398d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1399d763cef2SBarry Smith @*/ 14007087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1401d763cef2SBarry Smith { 1402d763cef2SBarry Smith PetscFunctionBegin; 14030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1404c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1405c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1406d763cef2SBarry Smith ts->max_steps = maxsteps; 1407d763cef2SBarry Smith ts->max_time = maxtime; 1408d763cef2SBarry Smith PetscFunctionReturn(0); 1409d763cef2SBarry Smith } 1410d763cef2SBarry Smith 14114a2ae208SSatish Balay #undef __FUNCT__ 14124a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1413d763cef2SBarry Smith /*@ 1414d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1415d763cef2SBarry Smith for use by the TS routines. 1416d763cef2SBarry Smith 14173f9fe445SBarry Smith Logically Collective on TS and Vec 1418d763cef2SBarry Smith 1419d763cef2SBarry Smith Input Parameters: 1420d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1421d763cef2SBarry Smith - x - the solution vector 1422d763cef2SBarry Smith 1423d763cef2SBarry Smith Level: beginner 1424d763cef2SBarry Smith 1425d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1426d763cef2SBarry Smith @*/ 14277087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1428d763cef2SBarry Smith { 14298737fe31SLisandro Dalcin PetscErrorCode ierr; 14308737fe31SLisandro Dalcin 1431d763cef2SBarry Smith PetscFunctionBegin; 14320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14330700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14348737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14358737fe31SLisandro Dalcin if (ts->vec_sol) {ierr = VecDestroy(ts->vec_sol);CHKERRQ(ierr);} 14368737fe31SLisandro Dalcin ts->vec_sol = x; 1437d763cef2SBarry Smith PetscFunctionReturn(0); 1438d763cef2SBarry Smith } 1439d763cef2SBarry Smith 1440e74ef692SMatthew Knepley #undef __FUNCT__ 1441e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1442ac226902SBarry Smith /*@C 1443000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14443f2090d5SJed Brown called once at the beginning of each time step. 1445000e7ae3SMatthew Knepley 14463f9fe445SBarry Smith Logically Collective on TS 1447000e7ae3SMatthew Knepley 1448000e7ae3SMatthew Knepley Input Parameters: 1449000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1450000e7ae3SMatthew Knepley - func - The function 1451000e7ae3SMatthew Knepley 1452000e7ae3SMatthew Knepley Calling sequence of func: 1453000e7ae3SMatthew Knepley . func (TS ts); 1454000e7ae3SMatthew Knepley 1455000e7ae3SMatthew Knepley Level: intermediate 1456000e7ae3SMatthew Knepley 1457000e7ae3SMatthew Knepley .keywords: TS, timestep 1458000e7ae3SMatthew Knepley @*/ 14597087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1460000e7ae3SMatthew Knepley { 1461000e7ae3SMatthew Knepley PetscFunctionBegin; 14620700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1463000e7ae3SMatthew Knepley ts->ops->prestep = func; 1464000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1465000e7ae3SMatthew Knepley } 1466000e7ae3SMatthew Knepley 1467e74ef692SMatthew Knepley #undef __FUNCT__ 14683f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 14693f2090d5SJed Brown /*@C 14703f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 14713f2090d5SJed Brown 14723f2090d5SJed Brown Collective on TS 14733f2090d5SJed Brown 14743f2090d5SJed Brown Input Parameters: 14753f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 14763f2090d5SJed Brown 14773f2090d5SJed Brown Notes: 14783f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 14793f2090d5SJed Brown so most users would not generally call this routine themselves. 14803f2090d5SJed Brown 14813f2090d5SJed Brown Level: developer 14823f2090d5SJed Brown 14833f2090d5SJed Brown .keywords: TS, timestep 14843f2090d5SJed Brown @*/ 14857087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 14863f2090d5SJed Brown { 14873f2090d5SJed Brown PetscErrorCode ierr; 14883f2090d5SJed Brown 14893f2090d5SJed Brown PetscFunctionBegin; 14900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 149172ac3e02SJed Brown if (ts->ops->prestep) { 14923f2090d5SJed Brown PetscStackPush("TS PreStep function"); 14933f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 14943f2090d5SJed Brown PetscStackPop; 1495312ce896SJed Brown } 14963f2090d5SJed Brown PetscFunctionReturn(0); 14973f2090d5SJed Brown } 14983f2090d5SJed Brown 14993f2090d5SJed Brown #undef __FUNCT__ 1500e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1501ac226902SBarry Smith /*@C 1502000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15033f2090d5SJed Brown called once at the end of each time step. 1504000e7ae3SMatthew Knepley 15053f9fe445SBarry Smith Logically Collective on TS 1506000e7ae3SMatthew Knepley 1507000e7ae3SMatthew Knepley Input Parameters: 1508000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1509000e7ae3SMatthew Knepley - func - The function 1510000e7ae3SMatthew Knepley 1511000e7ae3SMatthew Knepley Calling sequence of func: 1512000e7ae3SMatthew Knepley . func (TS ts); 1513000e7ae3SMatthew Knepley 1514000e7ae3SMatthew Knepley Level: intermediate 1515000e7ae3SMatthew Knepley 1516000e7ae3SMatthew Knepley .keywords: TS, timestep 1517000e7ae3SMatthew Knepley @*/ 15187087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1519000e7ae3SMatthew Knepley { 1520000e7ae3SMatthew Knepley PetscFunctionBegin; 15210700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1522000e7ae3SMatthew Knepley ts->ops->poststep = func; 1523000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1524000e7ae3SMatthew Knepley } 1525000e7ae3SMatthew Knepley 1526e74ef692SMatthew Knepley #undef __FUNCT__ 15273f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15283f2090d5SJed Brown /*@C 15293f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15303f2090d5SJed Brown 15313f2090d5SJed Brown Collective on TS 15323f2090d5SJed Brown 15333f2090d5SJed Brown Input Parameters: 15343f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15353f2090d5SJed Brown 15363f2090d5SJed Brown Notes: 15373f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15383f2090d5SJed Brown so most users would not generally call this routine themselves. 15393f2090d5SJed Brown 15403f2090d5SJed Brown Level: developer 15413f2090d5SJed Brown 15423f2090d5SJed Brown .keywords: TS, timestep 15433f2090d5SJed Brown @*/ 15447087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15453f2090d5SJed Brown { 15463f2090d5SJed Brown PetscErrorCode ierr; 15473f2090d5SJed Brown 15483f2090d5SJed Brown PetscFunctionBegin; 15490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 155072ac3e02SJed Brown if (ts->ops->poststep) { 15513f2090d5SJed Brown PetscStackPush("TS PostStep function"); 15523f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 15533f2090d5SJed Brown PetscStackPop; 155472ac3e02SJed Brown } 15553f2090d5SJed Brown PetscFunctionReturn(0); 15563f2090d5SJed Brown } 15573f2090d5SJed Brown 1558d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1559d763cef2SBarry Smith 15604a2ae208SSatish Balay #undef __FUNCT__ 1561a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1562d763cef2SBarry Smith /*@C 1563a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1564d763cef2SBarry Smith timestep to display the iteration's progress. 1565d763cef2SBarry Smith 15663f9fe445SBarry Smith Logically Collective on TS 1567d763cef2SBarry Smith 1568d763cef2SBarry Smith Input Parameters: 1569d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1570d763cef2SBarry Smith . func - monitoring routine 1571329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1572b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1573b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1574b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1575d763cef2SBarry Smith 1576d763cef2SBarry Smith Calling sequence of func: 1577a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1578d763cef2SBarry Smith 1579d763cef2SBarry Smith + ts - the TS context 1580d763cef2SBarry Smith . steps - iteration number 15811f06c33eSBarry Smith . time - current time 1582d763cef2SBarry Smith . x - current iterate 1583d763cef2SBarry Smith - mctx - [optional] monitoring context 1584d763cef2SBarry Smith 1585d763cef2SBarry Smith Notes: 1586d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1587d763cef2SBarry Smith already has been loaded. 1588d763cef2SBarry Smith 1589025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1590025f1a04SBarry Smith 1591d763cef2SBarry Smith Level: intermediate 1592d763cef2SBarry Smith 1593d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1594d763cef2SBarry Smith 1595a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1596d763cef2SBarry Smith @*/ 15977087cfbeSBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void*)) 1598d763cef2SBarry Smith { 1599d763cef2SBarry Smith PetscFunctionBegin; 16000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 160117186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1602d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1603329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1604d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1605d763cef2SBarry Smith PetscFunctionReturn(0); 1606d763cef2SBarry Smith } 1607d763cef2SBarry Smith 16084a2ae208SSatish Balay #undef __FUNCT__ 1609a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1610d763cef2SBarry Smith /*@C 1611a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1612d763cef2SBarry Smith 16133f9fe445SBarry Smith Logically Collective on TS 1614d763cef2SBarry Smith 1615d763cef2SBarry Smith Input Parameters: 1616d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1617d763cef2SBarry Smith 1618d763cef2SBarry Smith Notes: 1619d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1620d763cef2SBarry Smith 1621d763cef2SBarry Smith Level: intermediate 1622d763cef2SBarry Smith 1623d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1624d763cef2SBarry Smith 1625a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1626d763cef2SBarry Smith @*/ 16277087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1628d763cef2SBarry Smith { 1629d952e501SBarry Smith PetscErrorCode ierr; 1630d952e501SBarry Smith PetscInt i; 1631d952e501SBarry Smith 1632d763cef2SBarry Smith PetscFunctionBegin; 16330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1634d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1635d952e501SBarry Smith if (ts->mdestroy[i]) { 1636d952e501SBarry Smith ierr = (*ts->mdestroy[i])(ts->monitorcontext[i]);CHKERRQ(ierr); 1637d952e501SBarry Smith } 1638d952e501SBarry Smith } 1639d763cef2SBarry Smith ts->numbermonitors = 0; 1640d763cef2SBarry Smith PetscFunctionReturn(0); 1641d763cef2SBarry Smith } 1642d763cef2SBarry Smith 16434a2ae208SSatish Balay #undef __FUNCT__ 1644a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1645d8e5e3e6SSatish Balay /*@ 1646a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16475516499fSSatish Balay 16485516499fSSatish Balay Level: intermediate 164941251cbbSSatish Balay 16505516499fSSatish Balay .keywords: TS, set, monitor 16515516499fSSatish Balay 165241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 165341251cbbSSatish Balay @*/ 1654a6570f20SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *ctx) 1655d763cef2SBarry Smith { 1656dfbe8321SBarry Smith PetscErrorCode ierr; 1657a34d58ebSBarry Smith PetscViewerASCIIMonitor viewer = (PetscViewerASCIIMonitor)ctx; 1658d132466eSBarry Smith 1659d763cef2SBarry Smith PetscFunctionBegin; 1660a34d58ebSBarry Smith if (!ctx) { 16617adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)ts)->comm,"stdout",0,&viewer);CHKERRQ(ierr); 1662a34d58ebSBarry Smith } 1663f22f69f0SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1664a34d58ebSBarry Smith if (!ctx) { 1665a34d58ebSBarry Smith ierr = PetscViewerASCIIMonitorDestroy(viewer);CHKERRQ(ierr); 1666a34d58ebSBarry Smith } 1667d763cef2SBarry Smith PetscFunctionReturn(0); 1668d763cef2SBarry Smith } 1669d763cef2SBarry Smith 16704a2ae208SSatish Balay #undef __FUNCT__ 16714a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1672d763cef2SBarry Smith /*@ 1673d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1674d763cef2SBarry Smith 1675d763cef2SBarry Smith Collective on TS 1676d763cef2SBarry Smith 1677d763cef2SBarry Smith Input Parameter: 1678d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1679d763cef2SBarry Smith 1680d763cef2SBarry Smith Output Parameters: 1681d763cef2SBarry Smith + steps - number of iterations until termination 1682142b95e3SSatish Balay - ptime - time until termination 1683d763cef2SBarry Smith 1684d763cef2SBarry Smith Level: beginner 1685d763cef2SBarry Smith 1686d763cef2SBarry Smith .keywords: TS, timestep, solve 1687d763cef2SBarry Smith 1688d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1689d763cef2SBarry Smith @*/ 16907087cfbeSBarry Smith PetscErrorCode TSStep(TS ts,PetscInt *steps,PetscReal *ptime) 1691d763cef2SBarry Smith { 1692dfbe8321SBarry Smith PetscErrorCode ierr; 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith PetscFunctionBegin; 16950700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1696*277b19d0SLisandro Dalcin 1697d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1698d405a339SMatthew Knepley 1699d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1700000e7ae3SMatthew Knepley ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr); 1701d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1702d405a339SMatthew Knepley 17034bb05414SBarry Smith if (!PetscPreLoadingOn) { 17047adad957SLisandro Dalcin ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1705d405a339SMatthew Knepley } 1706d763cef2SBarry Smith PetscFunctionReturn(0); 1707d763cef2SBarry Smith } 1708d763cef2SBarry Smith 17094a2ae208SSatish Balay #undef __FUNCT__ 17106a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17116a4d4014SLisandro Dalcin /*@ 17126a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17136a4d4014SLisandro Dalcin 17146a4d4014SLisandro Dalcin Collective on TS 17156a4d4014SLisandro Dalcin 17166a4d4014SLisandro Dalcin Input Parameter: 17176a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17186a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17196a4d4014SLisandro Dalcin 17206a4d4014SLisandro Dalcin Level: beginner 17216a4d4014SLisandro Dalcin 17226a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17236a4d4014SLisandro Dalcin 17246a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17256a4d4014SLisandro Dalcin @*/ 17267087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17276a4d4014SLisandro Dalcin { 17286a4d4014SLisandro Dalcin PetscInt steps; 17296a4d4014SLisandro Dalcin PetscReal ptime; 17306a4d4014SLisandro Dalcin PetscErrorCode ierr; 1731f22f69f0SBarry Smith 17326a4d4014SLisandro Dalcin PetscFunctionBegin; 17330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17346a4d4014SLisandro Dalcin /* set solution vector if provided */ 17356a4d4014SLisandro Dalcin if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); } 17366a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 17376a4d4014SLisandro Dalcin ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0; 17386a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 17396a4d4014SLisandro Dalcin ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr); 17406a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17416a4d4014SLisandro Dalcin } 17426a4d4014SLisandro Dalcin 17436a4d4014SLisandro Dalcin #undef __FUNCT__ 17444a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1745d763cef2SBarry Smith /* 1746d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1747d763cef2SBarry Smith */ 1748a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1749d763cef2SBarry Smith { 17506849ba73SBarry Smith PetscErrorCode ierr; 1751a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1752d763cef2SBarry Smith 1753d763cef2SBarry Smith PetscFunctionBegin; 1754d763cef2SBarry Smith for (i=0; i<n; i++) { 1755142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1756d763cef2SBarry Smith } 1757d763cef2SBarry Smith PetscFunctionReturn(0); 1758d763cef2SBarry Smith } 1759d763cef2SBarry Smith 1760d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1761d763cef2SBarry Smith 17624a2ae208SSatish Balay #undef __FUNCT__ 1763a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1764d763cef2SBarry Smith /*@C 1765a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1766d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1767d763cef2SBarry Smith 1768d763cef2SBarry Smith Collective on TS 1769d763cef2SBarry Smith 1770d763cef2SBarry Smith Input Parameters: 1771d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1772d763cef2SBarry Smith . label - the title to put in the title bar 17737c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1774d763cef2SBarry Smith - m, n - the screen width and height in pixels 1775d763cef2SBarry Smith 1776d763cef2SBarry Smith Output Parameter: 1777d763cef2SBarry Smith . draw - the drawing context 1778d763cef2SBarry Smith 1779d763cef2SBarry Smith Options Database Key: 1780a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1781d763cef2SBarry Smith 1782d763cef2SBarry Smith Notes: 1783a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1784d763cef2SBarry Smith 1785d763cef2SBarry Smith Level: intermediate 1786d763cef2SBarry Smith 17877c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1788d763cef2SBarry Smith 1789a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 17907c922b88SBarry Smith 1791d763cef2SBarry Smith @*/ 17927087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1793d763cef2SBarry Smith { 1794b0a32e0cSBarry Smith PetscDraw win; 1795dfbe8321SBarry Smith PetscErrorCode ierr; 1796d763cef2SBarry Smith 1797d763cef2SBarry Smith PetscFunctionBegin; 1798b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1799b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1800b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1801b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1802d763cef2SBarry Smith 180352e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1804d763cef2SBarry Smith PetscFunctionReturn(0); 1805d763cef2SBarry Smith } 1806d763cef2SBarry Smith 18074a2ae208SSatish Balay #undef __FUNCT__ 1808a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1809a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1810d763cef2SBarry Smith { 1811b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 181287828ca2SBarry Smith PetscReal x,y = ptime; 1813dfbe8321SBarry Smith PetscErrorCode ierr; 1814d763cef2SBarry Smith 1815d763cef2SBarry Smith PetscFunctionBegin; 18167c922b88SBarry Smith if (!monctx) { 18177c922b88SBarry Smith MPI_Comm comm; 1818b0a32e0cSBarry Smith PetscViewer viewer; 18197c922b88SBarry Smith 18207c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1821b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1822b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18237c922b88SBarry Smith } 18247c922b88SBarry Smith 1825b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 182687828ca2SBarry Smith x = (PetscReal)n; 1827b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1828d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1829b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1830d763cef2SBarry Smith } 1831d763cef2SBarry Smith PetscFunctionReturn(0); 1832d763cef2SBarry Smith } 1833d763cef2SBarry Smith 18344a2ae208SSatish Balay #undef __FUNCT__ 1835a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1836d763cef2SBarry Smith /*@C 1837a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1838a6570f20SBarry Smith with TSMonitorLGCreate(). 1839d763cef2SBarry Smith 1840b0a32e0cSBarry Smith Collective on PetscDrawLG 1841d763cef2SBarry Smith 1842d763cef2SBarry Smith Input Parameter: 1843d763cef2SBarry Smith . draw - the drawing context 1844d763cef2SBarry Smith 1845d763cef2SBarry Smith Level: intermediate 1846d763cef2SBarry Smith 1847d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1848d763cef2SBarry Smith 1849a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1850d763cef2SBarry Smith @*/ 18517087cfbeSBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG drawlg) 1852d763cef2SBarry Smith { 1853b0a32e0cSBarry Smith PetscDraw draw; 1854dfbe8321SBarry Smith PetscErrorCode ierr; 1855d763cef2SBarry Smith 1856d763cef2SBarry Smith PetscFunctionBegin; 1857b0a32e0cSBarry Smith ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr); 1858b0a32e0cSBarry Smith ierr = PetscDrawDestroy(draw);CHKERRQ(ierr); 1859b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1860d763cef2SBarry Smith PetscFunctionReturn(0); 1861d763cef2SBarry Smith } 1862d763cef2SBarry Smith 18634a2ae208SSatish Balay #undef __FUNCT__ 18644a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1865d763cef2SBarry Smith /*@ 1866d763cef2SBarry Smith TSGetTime - Gets the current time. 1867d763cef2SBarry Smith 1868d763cef2SBarry Smith Not Collective 1869d763cef2SBarry Smith 1870d763cef2SBarry Smith Input Parameter: 1871d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith Output Parameter: 1874d763cef2SBarry Smith . t - the current time 1875d763cef2SBarry Smith 1876d763cef2SBarry Smith Level: beginner 1877d763cef2SBarry Smith 1878d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith .keywords: TS, get, time 1881d763cef2SBarry Smith @*/ 18827087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1883d763cef2SBarry Smith { 1884d763cef2SBarry Smith PetscFunctionBegin; 18850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18864482741eSBarry Smith PetscValidDoublePointer(t,2); 1887d763cef2SBarry Smith *t = ts->ptime; 1888d763cef2SBarry Smith PetscFunctionReturn(0); 1889d763cef2SBarry Smith } 1890d763cef2SBarry Smith 18914a2ae208SSatish Balay #undef __FUNCT__ 18926a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 18936a4d4014SLisandro Dalcin /*@ 18946a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 18956a4d4014SLisandro Dalcin 18963f9fe445SBarry Smith Logically Collective on TS 18976a4d4014SLisandro Dalcin 18986a4d4014SLisandro Dalcin Input Parameters: 18996a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19006a4d4014SLisandro Dalcin - time - the time 19016a4d4014SLisandro Dalcin 19026a4d4014SLisandro Dalcin Level: intermediate 19036a4d4014SLisandro Dalcin 19046a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19056a4d4014SLisandro Dalcin 19066a4d4014SLisandro Dalcin .keywords: TS, set, time 19076a4d4014SLisandro Dalcin @*/ 19087087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19096a4d4014SLisandro Dalcin { 19106a4d4014SLisandro Dalcin PetscFunctionBegin; 19110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1912c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19136a4d4014SLisandro Dalcin ts->ptime = t; 19146a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19156a4d4014SLisandro Dalcin } 19166a4d4014SLisandro Dalcin 19176a4d4014SLisandro Dalcin #undef __FUNCT__ 19184a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1919d763cef2SBarry Smith /*@C 1920d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1921d763cef2SBarry Smith TS options in the database. 1922d763cef2SBarry Smith 19233f9fe445SBarry Smith Logically Collective on TS 1924d763cef2SBarry Smith 1925d763cef2SBarry Smith Input Parameter: 1926d763cef2SBarry Smith + ts - The TS context 1927d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1928d763cef2SBarry Smith 1929d763cef2SBarry Smith Notes: 1930d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1931d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1932d763cef2SBarry Smith hyphen. 1933d763cef2SBarry Smith 1934d763cef2SBarry Smith Level: advanced 1935d763cef2SBarry Smith 1936d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1937d763cef2SBarry Smith 1938d763cef2SBarry Smith .seealso: TSSetFromOptions() 1939d763cef2SBarry Smith 1940d763cef2SBarry Smith @*/ 19417087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1942d763cef2SBarry Smith { 1943dfbe8321SBarry Smith PetscErrorCode ierr; 1944d763cef2SBarry Smith 1945d763cef2SBarry Smith PetscFunctionBegin; 19460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1947d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1948d763cef2SBarry Smith switch(ts->problem_type) { 1949d763cef2SBarry Smith case TS_NONLINEAR: 195059580b9cSBarry Smith if (ts->snes) { 1951d763cef2SBarry Smith ierr = SNESSetOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr); 195259580b9cSBarry Smith } 1953d763cef2SBarry Smith break; 1954d763cef2SBarry Smith case TS_LINEAR: 195559580b9cSBarry Smith if (ts->ksp) { 195694b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(ts->ksp,prefix);CHKERRQ(ierr); 195759580b9cSBarry Smith } 1958d763cef2SBarry Smith break; 1959d763cef2SBarry Smith } 1960d763cef2SBarry Smith PetscFunctionReturn(0); 1961d763cef2SBarry Smith } 1962d763cef2SBarry Smith 1963d763cef2SBarry Smith 19644a2ae208SSatish Balay #undef __FUNCT__ 19654a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 1966d763cef2SBarry Smith /*@C 1967d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 1968d763cef2SBarry Smith TS options in the database. 1969d763cef2SBarry Smith 19703f9fe445SBarry Smith Logically Collective on TS 1971d763cef2SBarry Smith 1972d763cef2SBarry Smith Input Parameter: 1973d763cef2SBarry Smith + ts - The TS context 1974d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith Notes: 1977d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1978d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1979d763cef2SBarry Smith hyphen. 1980d763cef2SBarry Smith 1981d763cef2SBarry Smith Level: advanced 1982d763cef2SBarry Smith 1983d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 1984d763cef2SBarry Smith 1985d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 1986d763cef2SBarry Smith 1987d763cef2SBarry Smith @*/ 19887087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 1989d763cef2SBarry Smith { 1990dfbe8321SBarry Smith PetscErrorCode ierr; 1991d763cef2SBarry Smith 1992d763cef2SBarry Smith PetscFunctionBegin; 19930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1994d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1995d763cef2SBarry Smith switch(ts->problem_type) { 1996d763cef2SBarry Smith case TS_NONLINEAR: 19971ac94b3bSBarry Smith if (ts->snes) { 1998d763cef2SBarry Smith ierr = SNESAppendOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr); 19991ac94b3bSBarry Smith } 2000d763cef2SBarry Smith break; 2001d763cef2SBarry Smith case TS_LINEAR: 20021ac94b3bSBarry Smith if (ts->ksp) { 200394b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(ts->ksp,prefix);CHKERRQ(ierr); 20041ac94b3bSBarry Smith } 2005d763cef2SBarry Smith break; 2006d763cef2SBarry Smith } 2007d763cef2SBarry Smith PetscFunctionReturn(0); 2008d763cef2SBarry Smith } 2009d763cef2SBarry Smith 20104a2ae208SSatish Balay #undef __FUNCT__ 20114a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2012d763cef2SBarry Smith /*@C 2013d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2014d763cef2SBarry Smith TS options in the database. 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith Not Collective 2017d763cef2SBarry Smith 2018d763cef2SBarry Smith Input Parameter: 2019d763cef2SBarry Smith . ts - The TS context 2020d763cef2SBarry Smith 2021d763cef2SBarry Smith Output Parameter: 2022d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2025d763cef2SBarry Smith sufficient length to hold the prefix. 2026d763cef2SBarry Smith 2027d763cef2SBarry Smith Level: intermediate 2028d763cef2SBarry Smith 2029d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2030d763cef2SBarry Smith 2031d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2032d763cef2SBarry Smith @*/ 20337087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2034d763cef2SBarry Smith { 2035dfbe8321SBarry Smith PetscErrorCode ierr; 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith PetscFunctionBegin; 20380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20394482741eSBarry Smith PetscValidPointer(prefix,2); 2040d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2041d763cef2SBarry Smith PetscFunctionReturn(0); 2042d763cef2SBarry Smith } 2043d763cef2SBarry Smith 20444a2ae208SSatish Balay #undef __FUNCT__ 20454a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2046d763cef2SBarry Smith /*@C 2047d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2048d763cef2SBarry Smith 2049d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Input Parameter: 2052d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2053d763cef2SBarry Smith 2054d763cef2SBarry Smith Output Parameters: 2055d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2056d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2057d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2060d763cef2SBarry Smith 2061d763cef2SBarry Smith Level: intermediate 2062d763cef2SBarry Smith 206326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2064d763cef2SBarry Smith 2065d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2066d763cef2SBarry Smith @*/ 20677087cfbeSBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,void **ctx) 2068d763cef2SBarry Smith { 2069d763cef2SBarry Smith PetscFunctionBegin; 207026d46c62SHong Zhang if (J) *J = ts->Arhs; 207126d46c62SHong Zhang if (M) *M = ts->B; 207226d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2073d763cef2SBarry Smith PetscFunctionReturn(0); 2074d763cef2SBarry Smith } 2075d763cef2SBarry Smith 20761713a123SBarry Smith #undef __FUNCT__ 20772eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 20782eca1d9cSJed Brown /*@C 20792eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 20802eca1d9cSJed Brown 20812eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 20822eca1d9cSJed Brown 20832eca1d9cSJed Brown Input Parameter: 20842eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 20852eca1d9cSJed Brown 20862eca1d9cSJed Brown Output Parameters: 20872eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 20882eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 20892eca1d9cSJed Brown . f - The function to compute the matrices 20902eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 20912eca1d9cSJed Brown 20922eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 20932eca1d9cSJed Brown 20942eca1d9cSJed Brown Level: advanced 20952eca1d9cSJed Brown 20962eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 20972eca1d9cSJed Brown 20982eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 20992eca1d9cSJed Brown @*/ 21007087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21012eca1d9cSJed Brown { 21022eca1d9cSJed Brown PetscFunctionBegin; 21032eca1d9cSJed Brown if (A) *A = ts->A; 21042eca1d9cSJed Brown if (B) *B = ts->B; 21052eca1d9cSJed Brown if (f) *f = ts->ops->ijacobian; 21062eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21072eca1d9cSJed Brown PetscFunctionReturn(0); 21082eca1d9cSJed Brown } 21092eca1d9cSJed Brown 21102eca1d9cSJed Brown #undef __FUNCT__ 2111a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21121713a123SBarry Smith /*@C 2113a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21141713a123SBarry Smith VecView() for the solution at each timestep 21151713a123SBarry Smith 21161713a123SBarry Smith Collective on TS 21171713a123SBarry Smith 21181713a123SBarry Smith Input Parameters: 21191713a123SBarry Smith + ts - the TS context 21201713a123SBarry Smith . step - current time-step 2121142b95e3SSatish Balay . ptime - current time 21221713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21231713a123SBarry Smith 21241713a123SBarry Smith Level: intermediate 21251713a123SBarry Smith 21261713a123SBarry Smith .keywords: TS, vector, monitor, view 21271713a123SBarry Smith 2128a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21291713a123SBarry Smith @*/ 21307087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21311713a123SBarry Smith { 2132dfbe8321SBarry Smith PetscErrorCode ierr; 21331713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21341713a123SBarry Smith 21351713a123SBarry Smith PetscFunctionBegin; 2136a34d58ebSBarry Smith if (!dummy) { 21377adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21381713a123SBarry Smith } 21391713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21401713a123SBarry Smith PetscFunctionReturn(0); 21411713a123SBarry Smith } 21421713a123SBarry Smith 21431713a123SBarry Smith 21446c699258SBarry Smith #undef __FUNCT__ 21456c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21466c699258SBarry Smith /*@ 21476c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21486c699258SBarry Smith 21493f9fe445SBarry Smith Logically Collective on TS and DM 21506c699258SBarry Smith 21516c699258SBarry Smith Input Parameters: 21526c699258SBarry Smith + ts - the preconditioner context 21536c699258SBarry Smith - dm - the dm 21546c699258SBarry Smith 21556c699258SBarry Smith Level: intermediate 21566c699258SBarry Smith 21576c699258SBarry Smith 21586c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 21596c699258SBarry Smith @*/ 21607087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 21616c699258SBarry Smith { 21626c699258SBarry Smith PetscErrorCode ierr; 21636c699258SBarry Smith 21646c699258SBarry Smith PetscFunctionBegin; 21650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 216670663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 21676c699258SBarry Smith if (ts->dm) {ierr = DMDestroy(ts->dm);CHKERRQ(ierr);} 21686c699258SBarry Smith ts->dm = dm; 216970663e4aSLisandro Dalcin if (ts->snes) { 217070663e4aSLisandro Dalcin ierr = SNESSetDM(ts->snes,dm);CHKERRQ(ierr); 217170663e4aSLisandro Dalcin } 217270663e4aSLisandro Dalcin if (ts->ksp) { 217370663e4aSLisandro Dalcin ierr = KSPSetDM(ts->ksp,dm);CHKERRQ(ierr); 217470663e4aSLisandro Dalcin ierr = KSPSetDMActive(ts->ksp,PETSC_FALSE);CHKERRQ(ierr); 217570663e4aSLisandro Dalcin } 21766c699258SBarry Smith PetscFunctionReturn(0); 21776c699258SBarry Smith } 21786c699258SBarry Smith 21796c699258SBarry Smith #undef __FUNCT__ 21806c699258SBarry Smith #define __FUNCT__ "TSGetDM" 21816c699258SBarry Smith /*@ 21826c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 21836c699258SBarry Smith 21843f9fe445SBarry Smith Not Collective 21856c699258SBarry Smith 21866c699258SBarry Smith Input Parameter: 21876c699258SBarry Smith . ts - the preconditioner context 21886c699258SBarry Smith 21896c699258SBarry Smith Output Parameter: 21906c699258SBarry Smith . dm - the dm 21916c699258SBarry Smith 21926c699258SBarry Smith Level: intermediate 21936c699258SBarry Smith 21946c699258SBarry Smith 21956c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 21966c699258SBarry Smith @*/ 21977087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 21986c699258SBarry Smith { 21996c699258SBarry Smith PetscFunctionBegin; 22000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22016c699258SBarry Smith *dm = ts->dm; 22026c699258SBarry Smith PetscFunctionReturn(0); 22036c699258SBarry Smith } 22041713a123SBarry Smith 22050f5c6efeSJed Brown #undef __FUNCT__ 22060f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22070f5c6efeSJed Brown /*@ 22080f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22090f5c6efeSJed Brown 22103f9fe445SBarry Smith Logically Collective on SNES 22110f5c6efeSJed Brown 22120f5c6efeSJed Brown Input Parameter: 2213d42a1c89SJed Brown + snes - nonlinear solver 22140f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2215d42a1c89SJed Brown - ctx - user context, must be a TS 22160f5c6efeSJed Brown 22170f5c6efeSJed Brown Output Parameter: 22180f5c6efeSJed Brown . F - the nonlinear residual 22190f5c6efeSJed Brown 22200f5c6efeSJed Brown Notes: 22210f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22220f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22230f5c6efeSJed Brown 22240f5c6efeSJed Brown Level: advanced 22250f5c6efeSJed Brown 22260f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22270f5c6efeSJed Brown @*/ 22287087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22290f5c6efeSJed Brown { 22300f5c6efeSJed Brown TS ts = (TS)ctx; 22310f5c6efeSJed Brown PetscErrorCode ierr; 22320f5c6efeSJed Brown 22330f5c6efeSJed Brown PetscFunctionBegin; 22340f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22350f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22360f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22370f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22380f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22390f5c6efeSJed Brown PetscFunctionReturn(0); 22400f5c6efeSJed Brown } 22410f5c6efeSJed Brown 22420f5c6efeSJed Brown #undef __FUNCT__ 22430f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22440f5c6efeSJed Brown /*@ 22450f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22460f5c6efeSJed Brown 22470f5c6efeSJed Brown Collective on SNES 22480f5c6efeSJed Brown 22490f5c6efeSJed Brown Input Parameter: 22500f5c6efeSJed Brown + snes - nonlinear solver 22510f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22520f5c6efeSJed Brown - ctx - user context, must be a TS 22530f5c6efeSJed Brown 22540f5c6efeSJed Brown Output Parameter: 22550f5c6efeSJed Brown + A - the Jacobian 22560f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 22570f5c6efeSJed Brown - flag - indicates any structure change in the matrix 22580f5c6efeSJed Brown 22590f5c6efeSJed Brown Notes: 22600f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22610f5c6efeSJed Brown 22620f5c6efeSJed Brown Level: developer 22630f5c6efeSJed Brown 22640f5c6efeSJed Brown .seealso: SNESSetJacobian() 22650f5c6efeSJed Brown @*/ 22667087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 22670f5c6efeSJed Brown { 22680f5c6efeSJed Brown TS ts = (TS)ctx; 22690f5c6efeSJed Brown PetscErrorCode ierr; 22700f5c6efeSJed Brown 22710f5c6efeSJed Brown PetscFunctionBegin; 22720f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22730f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22740f5c6efeSJed Brown PetscValidPointer(A,3); 22750f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 22760f5c6efeSJed Brown PetscValidPointer(B,4); 22770f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 22780f5c6efeSJed Brown PetscValidPointer(flag,5); 22790f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 22800f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 22810f5c6efeSJed Brown PetscFunctionReturn(0); 22820f5c6efeSJed Brown } 2283325fc9f4SBarry Smith 2284325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2285c6db04a5SJed Brown #include <mex.h> 2286325fc9f4SBarry Smith 2287325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2288325fc9f4SBarry Smith 2289325fc9f4SBarry Smith #undef __FUNCT__ 2290325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2291325fc9f4SBarry Smith /* 2292325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2293325fc9f4SBarry Smith TSSetFunctionMatlab(). 2294325fc9f4SBarry Smith 2295325fc9f4SBarry Smith Collective on TS 2296325fc9f4SBarry Smith 2297325fc9f4SBarry Smith Input Parameters: 2298325fc9f4SBarry Smith + snes - the TS context 2299325fc9f4SBarry Smith - x - input vector 2300325fc9f4SBarry Smith 2301325fc9f4SBarry Smith Output Parameter: 2302325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2303325fc9f4SBarry Smith 2304325fc9f4SBarry Smith Notes: 2305325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2306325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2307325fc9f4SBarry Smith themselves. 2308325fc9f4SBarry Smith 2309325fc9f4SBarry Smith Level: developer 2310325fc9f4SBarry Smith 2311325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2312325fc9f4SBarry Smith 2313325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2314325fc9f4SBarry Smith */ 23157087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2316325fc9f4SBarry Smith { 2317325fc9f4SBarry Smith PetscErrorCode ierr; 2318325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2319325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2320325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2321325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2322325fc9f4SBarry Smith 2323325fc9f4SBarry Smith PetscFunctionBegin; 2324325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2325325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2326325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2327325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2328325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2329325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2330325fc9f4SBarry Smith 2331325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2332325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2333880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2334325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2335325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2336325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2337325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2338325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2339325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2340325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2341325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2342325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2343325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2344325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2345325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2346325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2347325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2348325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2349325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2350325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2351325fc9f4SBarry Smith PetscFunctionReturn(0); 2352325fc9f4SBarry Smith } 2353325fc9f4SBarry Smith 2354325fc9f4SBarry Smith 2355325fc9f4SBarry Smith #undef __FUNCT__ 2356325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2357325fc9f4SBarry Smith /* 2358325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2359325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2360e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2361325fc9f4SBarry Smith 2362325fc9f4SBarry Smith Logically Collective on TS 2363325fc9f4SBarry Smith 2364325fc9f4SBarry Smith Input Parameters: 2365325fc9f4SBarry Smith + ts - the TS context 2366325fc9f4SBarry Smith - func - function evaluation routine 2367325fc9f4SBarry Smith 2368325fc9f4SBarry Smith Calling sequence of func: 2369325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2370325fc9f4SBarry Smith 2371325fc9f4SBarry Smith Level: beginner 2372325fc9f4SBarry Smith 2373325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2374325fc9f4SBarry Smith 2375325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2376325fc9f4SBarry Smith */ 23777087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2378325fc9f4SBarry Smith { 2379325fc9f4SBarry Smith PetscErrorCode ierr; 2380325fc9f4SBarry Smith TSMatlabContext *sctx; 2381325fc9f4SBarry Smith 2382325fc9f4SBarry Smith PetscFunctionBegin; 2383325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2384325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2385325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2386325fc9f4SBarry Smith /* 2387325fc9f4SBarry Smith This should work, but it doesn't 2388325fc9f4SBarry Smith sctx->ctx = ctx; 2389325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2390325fc9f4SBarry Smith */ 2391325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2392325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2393325fc9f4SBarry Smith PetscFunctionReturn(0); 2394325fc9f4SBarry Smith } 2395325fc9f4SBarry Smith 2396325fc9f4SBarry Smith #undef __FUNCT__ 2397325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2398325fc9f4SBarry Smith /* 2399325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2400325fc9f4SBarry Smith TSSetJacobianMatlab(). 2401325fc9f4SBarry Smith 2402325fc9f4SBarry Smith Collective on TS 2403325fc9f4SBarry Smith 2404325fc9f4SBarry Smith Input Parameters: 2405325fc9f4SBarry Smith + snes - the TS context 2406325fc9f4SBarry Smith . x - input vector 2407325fc9f4SBarry Smith . A, B - the matrices 2408325fc9f4SBarry Smith - ctx - user context 2409325fc9f4SBarry Smith 2410325fc9f4SBarry Smith Output Parameter: 2411325fc9f4SBarry Smith . flag - structure of the matrix 2412325fc9f4SBarry Smith 2413325fc9f4SBarry Smith Level: developer 2414325fc9f4SBarry Smith 2415325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2416325fc9f4SBarry Smith 2417325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2418325fc9f4SBarry Smith @*/ 24197087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2420325fc9f4SBarry Smith { 2421325fc9f4SBarry Smith PetscErrorCode ierr; 2422325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2423325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2424325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2425325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2426325fc9f4SBarry Smith 2427325fc9f4SBarry Smith PetscFunctionBegin; 2428325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2429325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2430325fc9f4SBarry Smith 2431325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2432325fc9f4SBarry Smith 2433325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2434325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2435325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2436325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2437325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2438325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2439325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2440325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2441325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2442325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2443325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2444325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2445325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2446325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2447325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2448325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2449325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2450325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2451325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2452325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2453325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2454325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2455325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2456325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2457325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2458325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2459325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2460325fc9f4SBarry Smith PetscFunctionReturn(0); 2461325fc9f4SBarry Smith } 2462325fc9f4SBarry Smith 2463325fc9f4SBarry Smith 2464325fc9f4SBarry Smith #undef __FUNCT__ 2465325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2466325fc9f4SBarry Smith /* 2467325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2468e3c5b3baSBarry 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 2469325fc9f4SBarry Smith 2470325fc9f4SBarry Smith Logically Collective on TS 2471325fc9f4SBarry Smith 2472325fc9f4SBarry Smith Input Parameters: 2473325fc9f4SBarry Smith + snes - the TS context 2474325fc9f4SBarry Smith . A,B - Jacobian matrices 2475325fc9f4SBarry Smith . func - function evaluation routine 2476325fc9f4SBarry Smith - ctx - user context 2477325fc9f4SBarry Smith 2478325fc9f4SBarry Smith Calling sequence of func: 2479325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2480325fc9f4SBarry Smith 2481325fc9f4SBarry Smith 2482325fc9f4SBarry Smith Level: developer 2483325fc9f4SBarry Smith 2484325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2485325fc9f4SBarry Smith 2486325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2487325fc9f4SBarry Smith */ 24887087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2489325fc9f4SBarry Smith { 2490325fc9f4SBarry Smith PetscErrorCode ierr; 2491325fc9f4SBarry Smith TSMatlabContext *sctx; 2492325fc9f4SBarry Smith 2493325fc9f4SBarry Smith PetscFunctionBegin; 2494325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2495325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2496325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2497325fc9f4SBarry Smith /* 2498325fc9f4SBarry Smith This should work, but it doesn't 2499325fc9f4SBarry Smith sctx->ctx = ctx; 2500325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2501325fc9f4SBarry Smith */ 2502325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2503325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2504325fc9f4SBarry Smith PetscFunctionReturn(0); 2505325fc9f4SBarry Smith } 2506325fc9f4SBarry Smith 2507b5b1a830SBarry Smith #undef __FUNCT__ 2508b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2509b5b1a830SBarry Smith /* 2510b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2511b5b1a830SBarry Smith 2512b5b1a830SBarry Smith Collective on TS 2513b5b1a830SBarry Smith 2514b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2515b5b1a830SBarry Smith @*/ 25167087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2517b5b1a830SBarry Smith { 2518b5b1a830SBarry Smith PetscErrorCode ierr; 2519b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2520a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2521b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2522b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2523b5b1a830SBarry Smith 2524b5b1a830SBarry Smith PetscFunctionBegin; 2525b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2526b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2527b5b1a830SBarry Smith 2528b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2529b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2530b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2531b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2532b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2533b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2534b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2535b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2536b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2537b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2538b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2539b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2540b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2541b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2542b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2543b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2544b5b1a830SBarry Smith PetscFunctionReturn(0); 2545b5b1a830SBarry Smith } 2546b5b1a830SBarry Smith 2547b5b1a830SBarry Smith 2548b5b1a830SBarry Smith #undef __FUNCT__ 2549b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2550b5b1a830SBarry Smith /* 2551b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2552b5b1a830SBarry Smith 2553b5b1a830SBarry Smith Level: developer 2554b5b1a830SBarry Smith 2555b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2556b5b1a830SBarry Smith 2557b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2558b5b1a830SBarry Smith */ 25597087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2560b5b1a830SBarry Smith { 2561b5b1a830SBarry Smith PetscErrorCode ierr; 2562b5b1a830SBarry Smith TSMatlabContext *sctx; 2563b5b1a830SBarry Smith 2564b5b1a830SBarry Smith PetscFunctionBegin; 2565b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2566b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2567b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2568b5b1a830SBarry Smith /* 2569b5b1a830SBarry Smith This should work, but it doesn't 2570b5b1a830SBarry Smith sctx->ctx = ctx; 2571b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2572b5b1a830SBarry Smith */ 2573b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2574b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2575b5b1a830SBarry Smith PetscFunctionReturn(0); 2576b5b1a830SBarry Smith } 2577b5b1a830SBarry Smith 2578325fc9f4SBarry Smith #endif 2579