163dd3a1aSKris Buschelman 2c6db04a5SJed Brown #include <private/tsimpl.h> /*I "petscts.h" I*/ 3d763cef2SBarry Smith 4d5ba7fb7SMatthew Knepley /* Logging support */ 57087cfbeSBarry Smith PetscClassId TS_CLASSID; 6166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 7d405a339SMatthew Knepley 84a2ae208SSatish Balay #undef __FUNCT__ 9bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 10bdad233fSMatthew Knepley /* 11bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 12bdad233fSMatthew Knepley 13bdad233fSMatthew Knepley Collective on TS 14bdad233fSMatthew Knepley 15bdad233fSMatthew Knepley Input Parameter: 16bdad233fSMatthew Knepley . ts - The ts 17bdad233fSMatthew Knepley 18bdad233fSMatthew Knepley Level: intermediate 19bdad233fSMatthew Knepley 20bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 21bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 22bdad233fSMatthew Knepley */ 236849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 24bdad233fSMatthew Knepley { 25ace3abfcSBarry Smith PetscBool opt; 262fc52814SBarry Smith const char *defaultType; 27bdad233fSMatthew Knepley char typeName[256]; 28dfbe8321SBarry Smith PetscErrorCode ierr; 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley PetscFunctionBegin; 317adad957SLisandro Dalcin if (((PetscObject)ts)->type_name) { 327adad957SLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 33bdad233fSMatthew Knepley } else { 349596e0b4SJed Brown defaultType = TSEULER; 35bdad233fSMatthew Knepley } 36bdad233fSMatthew Knepley 37cce0b1b2SLisandro Dalcin if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 38bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 39a7cc72afSBarry Smith if (opt) { 40bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 41bdad233fSMatthew Knepley } else { 42bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 43bdad233fSMatthew Knepley } 44bdad233fSMatthew Knepley PetscFunctionReturn(0); 45bdad233fSMatthew Knepley } 46bdad233fSMatthew Knepley 47bdad233fSMatthew Knepley #undef __FUNCT__ 48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 49bdad233fSMatthew Knepley /*@ 50bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 51bdad233fSMatthew Knepley 52bdad233fSMatthew Knepley Collective on TS 53bdad233fSMatthew Knepley 54bdad233fSMatthew Knepley Input Parameter: 55bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 56bdad233fSMatthew Knepley 57bdad233fSMatthew Knepley Options Database Keys: 584d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 59bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 60bdad233fSMatthew Knepley . -ts_max_time time - maximum time to compute to 61bdad233fSMatthew Knepley . -ts_dt dt - initial time step 62bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 63a6570f20SBarry Smith - -ts_monitor_draw - plot information at each timestep 64bdad233fSMatthew Knepley 65bdad233fSMatthew Knepley Level: beginner 66bdad233fSMatthew Knepley 67bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 68bdad233fSMatthew Knepley 69a313700dSBarry Smith .seealso: TSGetType() 70bdad233fSMatthew Knepley @*/ 717087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 72bdad233fSMatthew Knepley { 73bdad233fSMatthew Knepley PetscReal dt; 74ace3abfcSBarry Smith PetscBool opt,flg; 75dfbe8321SBarry Smith PetscErrorCode ierr; 76649052a6SBarry Smith PetscViewer monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78089b2837SJed Brown SNES snes; 79bdad233fSMatthew Knepley 80bdad233fSMatthew Knepley PetscFunctionBegin; 810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 827adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr); 83bdad233fSMatthew Knepley 84bdad233fSMatthew Knepley /* Handle generic TS options */ 85bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 86bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 87bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr); 88bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr); 893daf2de8SJed Brown if (opt) {ierr = TSSetInitialTimeStep(ts,ts->ptime,dt);CHKERRQ(ierr);} 90193ac0bcSJed Brown ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr); 91193ac0bcSJed Brown ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr); 92193ac0bcSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 93bdad233fSMatthew Knepley 94bdad233fSMatthew Knepley /* Monitor options */ 95a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 96eabae89aSBarry Smith if (flg) { 97649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 98649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 99bdad233fSMatthew Knepley } 10090d69ab7SBarry Smith opt = PETSC_FALSE; 101acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 102a7cc72afSBarry Smith if (opt) { 103a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 104bdad233fSMatthew Knepley } 10590d69ab7SBarry Smith opt = PETSC_FALSE; 106acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 107a7cc72afSBarry Smith if (opt) { 108a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 109bdad233fSMatthew Knepley } 110bdad233fSMatthew Knepley 111bdad233fSMatthew Knepley /* Handle TS type options */ 112bdad233fSMatthew Knepley ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley /* Handle specific TS options */ 115abc0a331SBarry Smith if (ts->ops->setfromoptions) { 116bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 117bdad233fSMatthew Knepley } 1185d973c19SBarry Smith 1195d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1205d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 121bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 122bdad233fSMatthew Knepley 123089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 124bdad233fSMatthew Knepley /* Handle subobject options */ 125089b2837SJed Brown if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 126089b2837SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 127bdad233fSMatthew Knepley PetscFunctionReturn(0); 128bdad233fSMatthew Knepley } 129bdad233fSMatthew Knepley 130bdad233fSMatthew Knepley #undef __FUNCT__ 131bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions" 132bdad233fSMatthew Knepley /*@ 133bdad233fSMatthew Knepley TSViewFromOptions - This function visualizes the ts based upon user options. 134bdad233fSMatthew Knepley 135bdad233fSMatthew Knepley Collective on TS 136bdad233fSMatthew Knepley 137bdad233fSMatthew Knepley Input Parameter: 138bdad233fSMatthew Knepley . ts - The ts 139bdad233fSMatthew Knepley 140bdad233fSMatthew Knepley Level: intermediate 141bdad233fSMatthew Knepley 142bdad233fSMatthew Knepley .keywords: TS, view, options, database 143bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView() 144bdad233fSMatthew Knepley @*/ 1457087cfbeSBarry Smith PetscErrorCode TSViewFromOptions(TS ts,const char title[]) 146bdad233fSMatthew Knepley { 147bdad233fSMatthew Knepley PetscViewer viewer; 148bdad233fSMatthew Knepley PetscDraw draw; 149ace3abfcSBarry Smith PetscBool opt = PETSC_FALSE; 150e10c49a3SBarry Smith char fileName[PETSC_MAX_PATH_LEN]; 151dfbe8321SBarry Smith PetscErrorCode ierr; 152bdad233fSMatthew Knepley 153bdad233fSMatthew Knepley PetscFunctionBegin; 1547adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr); 155eabae89aSBarry Smith if (opt && !PetscPreLoadingOn) { 1567adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr); 157bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 1586bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 159bdad233fSMatthew Knepley } 1608e83347fSKai Germaschewski opt = PETSC_FALSE; 161acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr); 162a7cc72afSBarry Smith if (opt) { 1637adad957SLisandro Dalcin ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr); 164bdad233fSMatthew Knepley ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr); 165a7cc72afSBarry Smith if (title) { 1661836bdbcSSatish Balay ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr); 167bdad233fSMatthew Knepley } else { 168bdad233fSMatthew Knepley ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr); 1697adad957SLisandro Dalcin ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr); 170bdad233fSMatthew Knepley } 171bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 172bdad233fSMatthew Knepley ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 173bdad233fSMatthew Knepley ierr = PetscDrawPause(draw);CHKERRQ(ierr); 1746bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 175bdad233fSMatthew Knepley } 176bdad233fSMatthew Knepley PetscFunctionReturn(0); 177bdad233fSMatthew Knepley } 178bdad233fSMatthew Knepley 179bdad233fSMatthew Knepley #undef __FUNCT__ 1804a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 181a7a1495cSBarry Smith /*@ 1828c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 183a7a1495cSBarry Smith set with TSSetRHSJacobian(). 184a7a1495cSBarry Smith 185a7a1495cSBarry Smith Collective on TS and Vec 186a7a1495cSBarry Smith 187a7a1495cSBarry Smith Input Parameters: 188316643e7SJed Brown + ts - the TS context 189a7a1495cSBarry Smith . t - current timestep 190a7a1495cSBarry Smith - x - input vector 191a7a1495cSBarry Smith 192a7a1495cSBarry Smith Output Parameters: 193a7a1495cSBarry Smith + A - Jacobian matrix 194a7a1495cSBarry Smith . B - optional preconditioning matrix 195a7a1495cSBarry Smith - flag - flag indicating matrix structure 196a7a1495cSBarry Smith 197a7a1495cSBarry Smith Notes: 198a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 199a7a1495cSBarry Smith is used internally within the nonlinear solvers. 200a7a1495cSBarry Smith 20194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 202a7a1495cSBarry Smith flag parameter. 203a7a1495cSBarry Smith 204a7a1495cSBarry Smith Level: developer 205a7a1495cSBarry Smith 206a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 207a7a1495cSBarry Smith 20894b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 209a7a1495cSBarry Smith @*/ 2107087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 211a7a1495cSBarry Smith { 212dfbe8321SBarry Smith PetscErrorCode ierr; 213*0e4ef248SJed Brown PetscInt Xstate; 214a7a1495cSBarry Smith 215a7a1495cSBarry Smith PetscFunctionBegin; 2160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2170700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 218c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 219*0e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 220*0e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 221*0e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 222*0e4ef248SJed Brown PetscFunctionReturn(0); 223*0e4ef248SJed Brown } 224000e7ae3SMatthew Knepley if (ts->ops->rhsjacobian) { 225d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 226a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 227a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 228000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 229a7a1495cSBarry Smith PetscStackPop; 230d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 231a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2320700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2330700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 234ef66eb69SBarry Smith } else { 235214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 236214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 237214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 238ef66eb69SBarry Smith } 239*0e4ef248SJed Brown ts->rhsjacobian.time = t; 240*0e4ef248SJed Brown ts->rhsjacobian.X = X; 241*0e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 242*0e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 243a7a1495cSBarry Smith PetscFunctionReturn(0); 244a7a1495cSBarry Smith } 245a7a1495cSBarry Smith 2464a2ae208SSatish Balay #undef __FUNCT__ 2474a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 248316643e7SJed Brown /*@ 249d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 250d763cef2SBarry Smith 251316643e7SJed Brown Collective on TS and Vec 252316643e7SJed Brown 253316643e7SJed Brown Input Parameters: 254316643e7SJed Brown + ts - the TS context 255316643e7SJed Brown . t - current time 256316643e7SJed Brown - x - state vector 257316643e7SJed Brown 258316643e7SJed Brown Output Parameter: 259316643e7SJed Brown . y - right hand side 260316643e7SJed Brown 261316643e7SJed Brown Note: 262316643e7SJed Brown Most users should not need to explicitly call this routine, as it 263316643e7SJed Brown is used internally within the nonlinear solvers. 264316643e7SJed Brown 265316643e7SJed Brown Level: developer 266316643e7SJed Brown 267316643e7SJed Brown .keywords: TS, compute 268316643e7SJed Brown 269316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 270316643e7SJed Brown @*/ 271dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 272d763cef2SBarry Smith { 273dfbe8321SBarry Smith PetscErrorCode ierr; 274d763cef2SBarry Smith 275d763cef2SBarry Smith PetscFunctionBegin; 2760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2770700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2780700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 279d763cef2SBarry Smith 280d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 281000e7ae3SMatthew Knepley if (ts->ops->rhsfunction) { 282d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 283000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 284d763cef2SBarry Smith PetscStackPop; 285214bc6a2SJed Brown } else { 286214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 287b2cd27e8SJed Brown } 288d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 289d763cef2SBarry Smith PetscFunctionReturn(0); 290d763cef2SBarry Smith } 291d763cef2SBarry Smith 2924a2ae208SSatish Balay #undef __FUNCT__ 293214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 294214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 295214bc6a2SJed Brown { 296214bc6a2SJed Brown PetscErrorCode ierr; 297214bc6a2SJed Brown 298214bc6a2SJed Brown PetscFunctionBegin; 299214bc6a2SJed Brown if (!ts->Frhs) { 300214bc6a2SJed Brown ierr = VecDuplicate(ts->vec_sol,&ts->Frhs);CHKERRQ(ierr); 301214bc6a2SJed Brown } 302214bc6a2SJed Brown *Frhs = ts->Frhs; 303214bc6a2SJed Brown PetscFunctionReturn(0); 304214bc6a2SJed Brown } 305214bc6a2SJed Brown 306214bc6a2SJed Brown #undef __FUNCT__ 307214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 308214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 309214bc6a2SJed Brown { 310214bc6a2SJed Brown PetscErrorCode ierr; 311214bc6a2SJed Brown Mat A,B; 312214bc6a2SJed Brown 313214bc6a2SJed Brown PetscFunctionBegin; 314214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 315214bc6a2SJed Brown if (Arhs) { 316214bc6a2SJed Brown if (!ts->Arhs) { 317214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 318214bc6a2SJed Brown } 319214bc6a2SJed Brown *Arhs = ts->Arhs; 320214bc6a2SJed Brown } 321214bc6a2SJed Brown if (Brhs) { 322214bc6a2SJed Brown if (!ts->Brhs) { 323214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 324214bc6a2SJed Brown } 325214bc6a2SJed Brown *Brhs = ts->Brhs; 326214bc6a2SJed Brown } 327214bc6a2SJed Brown PetscFunctionReturn(0); 328214bc6a2SJed Brown } 329214bc6a2SJed Brown 330214bc6a2SJed Brown #undef __FUNCT__ 331316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 332316643e7SJed Brown /*@ 333316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 334316643e7SJed Brown 335316643e7SJed Brown Collective on TS and Vec 336316643e7SJed Brown 337316643e7SJed Brown Input Parameters: 338316643e7SJed Brown + ts - the TS context 339316643e7SJed Brown . t - current time 340316643e7SJed Brown . X - state vector 341214bc6a2SJed Brown . Xdot - time derivative of state vector 342214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 343316643e7SJed Brown 344316643e7SJed Brown Output Parameter: 345316643e7SJed Brown . Y - right hand side 346316643e7SJed Brown 347316643e7SJed Brown Note: 348316643e7SJed Brown Most users should not need to explicitly call this routine, as it 349316643e7SJed Brown is used internally within the nonlinear solvers. 350316643e7SJed Brown 351316643e7SJed Brown If the user did did not write their equations in implicit form, this 352316643e7SJed Brown function recasts them in implicit form. 353316643e7SJed Brown 354316643e7SJed Brown Level: developer 355316643e7SJed Brown 356316643e7SJed Brown .keywords: TS, compute 357316643e7SJed Brown 358316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 359316643e7SJed Brown @*/ 360214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 361316643e7SJed Brown { 362316643e7SJed Brown PetscErrorCode ierr; 363316643e7SJed Brown 364316643e7SJed Brown PetscFunctionBegin; 3650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3660700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3670700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3680700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 369316643e7SJed Brown 370316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 371316643e7SJed Brown if (ts->ops->ifunction) { 372316643e7SJed Brown PetscStackPush("TS user implicit function"); 373316643e7SJed Brown ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 374316643e7SJed Brown PetscStackPop; 375214bc6a2SJed Brown } 376214bc6a2SJed Brown if (imex) { 377214bc6a2SJed Brown if (!ts->ops->ifunction) {ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);} 378316643e7SJed Brown } else { 379214bc6a2SJed Brown if (!ts->ops->ifunction) { 380089b2837SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 381ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 382214bc6a2SJed Brown } else { 383214bc6a2SJed Brown Vec Frhs; 384214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 385214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 386214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 387316643e7SJed Brown } 3884a6899ffSJed Brown } 389316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 390316643e7SJed Brown PetscFunctionReturn(0); 391316643e7SJed Brown } 392316643e7SJed Brown 393316643e7SJed Brown #undef __FUNCT__ 394316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 395316643e7SJed Brown /*@ 396316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 397316643e7SJed Brown 398316643e7SJed Brown Collective on TS and Vec 399316643e7SJed Brown 400316643e7SJed Brown Input 401316643e7SJed Brown Input Parameters: 402316643e7SJed Brown + ts - the TS context 403316643e7SJed Brown . t - current timestep 404316643e7SJed Brown . X - state vector 405316643e7SJed Brown . Xdot - time derivative of state vector 406214bc6a2SJed Brown . shift - shift to apply, see note below 407214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 408316643e7SJed Brown 409316643e7SJed Brown Output Parameters: 410316643e7SJed Brown + A - Jacobian matrix 411316643e7SJed Brown . B - optional preconditioning matrix 412316643e7SJed Brown - flag - flag indicating matrix structure 413316643e7SJed Brown 414316643e7SJed Brown Notes: 415316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 416316643e7SJed Brown 417316643e7SJed Brown dF/dX + shift*dF/dXdot 418316643e7SJed Brown 419316643e7SJed Brown Most users should not need to explicitly call this routine, as it 420316643e7SJed Brown is used internally within the nonlinear solvers. 421316643e7SJed Brown 422316643e7SJed Brown Level: developer 423316643e7SJed Brown 424316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 425316643e7SJed Brown 426316643e7SJed Brown .seealso: TSSetIJacobian() 42763495f91SJed Brown @*/ 428214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 429316643e7SJed Brown { 430316643e7SJed Brown PetscErrorCode ierr; 431316643e7SJed Brown 432316643e7SJed Brown PetscFunctionBegin; 4330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4340700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4350700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 436316643e7SJed Brown PetscValidPointer(A,6); 4370700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 438316643e7SJed Brown PetscValidPointer(B,7); 4390700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 440316643e7SJed Brown PetscValidPointer(flg,8); 441316643e7SJed Brown 4424e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 443316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 444316643e7SJed Brown if (ts->ops->ijacobian) { 445316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 446316643e7SJed Brown ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 447316643e7SJed Brown PetscStackPop; 448214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 449214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 450214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 4514a6899ffSJed Brown } 452214bc6a2SJed Brown if (imex) { 453214bc6a2SJed Brown if (!ts->ops->ijacobian) { /* system was written as Xdot = F(t,X) */ 454214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 455214bc6a2SJed Brown ierr = MatShift(*A,1.0);CHKERRQ(ierr); 456214bc6a2SJed Brown if (*A != *B) { 457214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 458214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 459214bc6a2SJed Brown } 460214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 461214bc6a2SJed Brown } 462214bc6a2SJed Brown } else { 463214bc6a2SJed Brown if (!ts->ops->ijacobian) { 464214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 465214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 466214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 467316643e7SJed Brown if (*A != *B) { 468316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 469316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 470316643e7SJed Brown } 471214bc6a2SJed Brown } else if (ts->ops->rhsjacobian) { 472214bc6a2SJed Brown Mat Arhs,Brhs; 473214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 474214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 475214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 476214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 477214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 478214bc6a2SJed Brown if (*A != *B) { 479214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 480214bc6a2SJed Brown } 481214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 482214bc6a2SJed Brown } 483316643e7SJed Brown } 484316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 485316643e7SJed Brown PetscFunctionReturn(0); 486316643e7SJed Brown } 487316643e7SJed Brown 488316643e7SJed Brown #undef __FUNCT__ 4894a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 490d763cef2SBarry Smith /*@C 491d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 492d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 493d763cef2SBarry Smith 4943f9fe445SBarry Smith Logically Collective on TS 495d763cef2SBarry Smith 496d763cef2SBarry Smith Input Parameters: 497d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 498ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 499d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 500d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 501d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 502d763cef2SBarry Smith 503d763cef2SBarry Smith Calling sequence of func: 50487828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 505d763cef2SBarry Smith 506d763cef2SBarry Smith + t - current timestep 507d763cef2SBarry Smith . u - input vector 508d763cef2SBarry Smith . F - function vector 509d763cef2SBarry Smith - ctx - [optional] user-defined function context 510d763cef2SBarry Smith 511d763cef2SBarry Smith Important: 51276f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 513d763cef2SBarry Smith 514d763cef2SBarry Smith Level: beginner 515d763cef2SBarry Smith 516d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 517d763cef2SBarry Smith 51876f2fa84SHong Zhang .seealso: TSSetMatrices() 519d763cef2SBarry Smith @*/ 520089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 521d763cef2SBarry Smith { 522089b2837SJed Brown PetscErrorCode ierr; 523089b2837SJed Brown SNES snes; 524d763cef2SBarry Smith 525089b2837SJed Brown PetscFunctionBegin; 5260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 527ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 5284e684422SJed Brown if (f) ts->ops->rhsfunction = f; 5294e684422SJed Brown if (ctx) ts->funP = ctx; 530089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 531089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 532d763cef2SBarry Smith PetscFunctionReturn(0); 533d763cef2SBarry Smith } 534d763cef2SBarry Smith 5354a2ae208SSatish Balay #undef __FUNCT__ 5364a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 537d763cef2SBarry Smith /*@C 538d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 539d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 54076f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 541d763cef2SBarry Smith 5423f9fe445SBarry Smith Logically Collective on TS 543d763cef2SBarry Smith 544d763cef2SBarry Smith Input Parameters: 545d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 546d763cef2SBarry Smith . A - Jacobian matrix 547d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 548d763cef2SBarry Smith . f - the Jacobian evaluation routine 549d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 550d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 551d763cef2SBarry Smith 552d763cef2SBarry Smith Calling sequence of func: 55387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 554d763cef2SBarry Smith 555d763cef2SBarry Smith + t - current timestep 556d763cef2SBarry Smith . u - input vector 557d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 558d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 559d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 56094b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 561d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 562d763cef2SBarry Smith 563d763cef2SBarry Smith Notes: 56494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 565d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 566d763cef2SBarry Smith 567d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 568d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 56956335db2SHong Zhang completely new matrix structure (not just different matrix elements) 570d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 571d763cef2SBarry Smith throughout the global iterations. 572d763cef2SBarry Smith 573d763cef2SBarry Smith Level: beginner 574d763cef2SBarry Smith 575d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 576d763cef2SBarry Smith 577d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 57876f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 579d763cef2SBarry Smith 580d763cef2SBarry Smith @*/ 581089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 582d763cef2SBarry Smith { 583277b19d0SLisandro Dalcin PetscErrorCode ierr; 584089b2837SJed Brown SNES snes; 585277b19d0SLisandro Dalcin 586d763cef2SBarry Smith PetscFunctionBegin; 5870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 588277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 589277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 590277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 591277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 592d763cef2SBarry Smith 593277b19d0SLisandro Dalcin if (f) ts->ops->rhsjacobian = f; 594277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 595089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 596*0e4ef248SJed Brown if (!ts->ops->ijacobian) { 597089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 598*0e4ef248SJed Brown } 599*0e4ef248SJed Brown if (A) { 600*0e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 601*0e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 602*0e4ef248SJed Brown ts->Arhs = A; 603*0e4ef248SJed Brown } 604*0e4ef248SJed Brown if (B) { 605*0e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 606*0e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 607*0e4ef248SJed Brown ts->Brhs = B; 608*0e4ef248SJed Brown } 609d763cef2SBarry Smith PetscFunctionReturn(0); 610d763cef2SBarry Smith } 611d763cef2SBarry Smith 612316643e7SJed Brown 613316643e7SJed Brown #undef __FUNCT__ 614316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 615316643e7SJed Brown /*@C 616316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 617316643e7SJed Brown 6183f9fe445SBarry Smith Logically Collective on TS 619316643e7SJed Brown 620316643e7SJed Brown Input Parameters: 621316643e7SJed Brown + ts - the TS context obtained from TSCreate() 622ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 623316643e7SJed Brown . f - the function evaluation routine 624316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 625316643e7SJed Brown 626316643e7SJed Brown Calling sequence of f: 627316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 628316643e7SJed Brown 629316643e7SJed Brown + t - time at step/stage being solved 630316643e7SJed Brown . u - state vector 631316643e7SJed Brown . u_t - time derivative of state vector 632316643e7SJed Brown . F - function vector 633316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 634316643e7SJed Brown 635316643e7SJed Brown Important: 636316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 637316643e7SJed Brown 638316643e7SJed Brown Level: beginner 639316643e7SJed Brown 640316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 641316643e7SJed Brown 642316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 643316643e7SJed Brown @*/ 644089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 645316643e7SJed Brown { 646089b2837SJed Brown PetscErrorCode ierr; 647089b2837SJed Brown SNES snes; 648316643e7SJed Brown 649316643e7SJed Brown PetscFunctionBegin; 6500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 651ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 652089b2837SJed Brown if (f) ts->ops->ifunction = f; 653089b2837SJed Brown if (ctx) ts->funP = ctx; 654089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 655089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 656089b2837SJed Brown PetscFunctionReturn(0); 657089b2837SJed Brown } 658089b2837SJed Brown 659089b2837SJed Brown #undef __FUNCT__ 660089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 661089b2837SJed Brown /*@C 662089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 663089b2837SJed Brown 664089b2837SJed Brown Not Collective 665089b2837SJed Brown 666089b2837SJed Brown Input Parameter: 667089b2837SJed Brown . ts - the TS context 668089b2837SJed Brown 669089b2837SJed Brown Output Parameter: 670089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 671089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 672089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 673089b2837SJed Brown 674089b2837SJed Brown Level: advanced 675089b2837SJed Brown 676089b2837SJed Brown .keywords: TS, nonlinear, get, function 677089b2837SJed Brown 678089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 679089b2837SJed Brown @*/ 680089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 681089b2837SJed Brown { 682089b2837SJed Brown PetscErrorCode ierr; 683089b2837SJed Brown SNES snes; 684089b2837SJed Brown 685089b2837SJed Brown PetscFunctionBegin; 686089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 687089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 688089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 689089b2837SJed Brown if (func) *func = ts->ops->ifunction; 690089b2837SJed Brown if (ctx) *ctx = ts->funP; 691089b2837SJed Brown PetscFunctionReturn(0); 692089b2837SJed Brown } 693089b2837SJed Brown 694089b2837SJed Brown #undef __FUNCT__ 695089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 696089b2837SJed Brown /*@C 697089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 698089b2837SJed Brown 699089b2837SJed Brown Not Collective 700089b2837SJed Brown 701089b2837SJed Brown Input Parameter: 702089b2837SJed Brown . ts - the TS context 703089b2837SJed Brown 704089b2837SJed Brown Output Parameter: 705089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 706089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 707089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 708089b2837SJed Brown 709089b2837SJed Brown Level: advanced 710089b2837SJed Brown 711089b2837SJed Brown .keywords: TS, nonlinear, get, function 712089b2837SJed Brown 713089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 714089b2837SJed Brown @*/ 715089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 716089b2837SJed Brown { 717089b2837SJed Brown PetscErrorCode ierr; 718089b2837SJed Brown SNES snes; 719089b2837SJed Brown 720089b2837SJed Brown PetscFunctionBegin; 721089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 722089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 723089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 724089b2837SJed Brown if (func) *func = ts->ops->rhsfunction; 725089b2837SJed Brown if (ctx) *ctx = ts->funP; 726316643e7SJed Brown PetscFunctionReturn(0); 727316643e7SJed Brown } 728316643e7SJed Brown 729316643e7SJed Brown #undef __FUNCT__ 730316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 731316643e7SJed Brown /*@C 732a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 733a4f0a591SBarry Smith you provided with TSSetIFunction(). 734316643e7SJed Brown 7353f9fe445SBarry Smith Logically Collective on TS 736316643e7SJed Brown 737316643e7SJed Brown Input Parameters: 738316643e7SJed Brown + ts - the TS context obtained from TSCreate() 739316643e7SJed Brown . A - Jacobian matrix 740316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 741316643e7SJed Brown . f - the Jacobian evaluation routine 742316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 743316643e7SJed Brown 744316643e7SJed Brown Calling sequence of f: 7451b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 746316643e7SJed Brown 747316643e7SJed Brown + t - time at step/stage being solved 7481b4a444bSJed Brown . U - state vector 7491b4a444bSJed Brown . U_t - time derivative of state vector 750316643e7SJed Brown . a - shift 7511b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 752316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 753316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 754316643e7SJed Brown structure (same as flag in KSPSetOperators()) 755316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 756316643e7SJed Brown 757316643e7SJed Brown Notes: 758316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 759316643e7SJed Brown 760a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 761a4f0a591SBarry Smith the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 762a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 763a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 764a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 765a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 766a4f0a591SBarry Smith 767316643e7SJed Brown Level: beginner 768316643e7SJed Brown 769316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 770316643e7SJed Brown 771316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 772316643e7SJed Brown 773316643e7SJed Brown @*/ 7747087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 775316643e7SJed Brown { 776316643e7SJed Brown PetscErrorCode ierr; 777089b2837SJed Brown SNES snes; 778316643e7SJed Brown 779316643e7SJed Brown PetscFunctionBegin; 7800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7810700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 7820700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 783316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 784316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 785316643e7SJed Brown if (f) ts->ops->ijacobian = f; 786316643e7SJed Brown if (ctx) ts->jacP = ctx; 787089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 788089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 789316643e7SJed Brown PetscFunctionReturn(0); 790316643e7SJed Brown } 791316643e7SJed Brown 7924a2ae208SSatish Balay #undef __FUNCT__ 7934a2ae208SSatish Balay #define __FUNCT__ "TSView" 7947e2c5f70SBarry Smith /*@C 795d763cef2SBarry Smith TSView - Prints the TS data structure. 796d763cef2SBarry Smith 7974c49b128SBarry Smith Collective on TS 798d763cef2SBarry Smith 799d763cef2SBarry Smith Input Parameters: 800d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 801d763cef2SBarry Smith - viewer - visualization context 802d763cef2SBarry Smith 803d763cef2SBarry Smith Options Database Key: 804d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 805d763cef2SBarry Smith 806d763cef2SBarry Smith Notes: 807d763cef2SBarry Smith The available visualization contexts include 808b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 809b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 810d763cef2SBarry Smith output where only the first processor opens 811d763cef2SBarry Smith the file. All other processors send their 812d763cef2SBarry Smith data to the first processor to print. 813d763cef2SBarry Smith 814d763cef2SBarry Smith The user can open an alternative visualization context with 815b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 816d763cef2SBarry Smith 817d763cef2SBarry Smith Level: beginner 818d763cef2SBarry Smith 819d763cef2SBarry Smith .keywords: TS, timestep, view 820d763cef2SBarry Smith 821b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 822d763cef2SBarry Smith @*/ 8237087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 824d763cef2SBarry Smith { 825dfbe8321SBarry Smith PetscErrorCode ierr; 826a313700dSBarry Smith const TSType type; 827089b2837SJed Brown PetscBool iascii,isstring,isundials; 828d763cef2SBarry Smith 829d763cef2SBarry Smith PetscFunctionBegin; 8300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8313050cee2SBarry Smith if (!viewer) { 8327adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8333050cee2SBarry Smith } 8340700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 835c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 836fd16b177SBarry Smith 8372692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8382692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 83932077d6dSBarry Smith if (iascii) { 840317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 841000e7ae3SMatthew Knepley if (ts->ops->view) { 842b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 843000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 844b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 845d763cef2SBarry Smith } 84677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 847a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 848d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 84977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 850193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr); 851d763cef2SBarry Smith } 85277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 853193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 8540f5bd95cSBarry Smith } else if (isstring) { 855a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 856b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 857d763cef2SBarry Smith } 858b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 859089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 860089b2837SJed Brown if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 861b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 862d763cef2SBarry Smith PetscFunctionReturn(0); 863d763cef2SBarry Smith } 864d763cef2SBarry Smith 865d763cef2SBarry Smith 8664a2ae208SSatish Balay #undef __FUNCT__ 8674a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 868b07ff414SBarry Smith /*@ 869d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 870d763cef2SBarry Smith the timesteppers. 871d763cef2SBarry Smith 8723f9fe445SBarry Smith Logically Collective on TS 873d763cef2SBarry Smith 874d763cef2SBarry Smith Input Parameters: 875d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 876d763cef2SBarry Smith - usrP - optional user context 877d763cef2SBarry Smith 878d763cef2SBarry Smith Level: intermediate 879d763cef2SBarry Smith 880d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 881d763cef2SBarry Smith 882d763cef2SBarry Smith .seealso: TSGetApplicationContext() 883d763cef2SBarry Smith @*/ 8847087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 885d763cef2SBarry Smith { 886d763cef2SBarry Smith PetscFunctionBegin; 8870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 888d763cef2SBarry Smith ts->user = usrP; 889d763cef2SBarry Smith PetscFunctionReturn(0); 890d763cef2SBarry Smith } 891d763cef2SBarry Smith 8924a2ae208SSatish Balay #undef __FUNCT__ 8934a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 894b07ff414SBarry Smith /*@ 895d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 896d763cef2SBarry Smith timestepper. 897d763cef2SBarry Smith 898d763cef2SBarry Smith Not Collective 899d763cef2SBarry Smith 900d763cef2SBarry Smith Input Parameter: 901d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 902d763cef2SBarry Smith 903d763cef2SBarry Smith Output Parameter: 904d763cef2SBarry Smith . usrP - user context 905d763cef2SBarry Smith 906d763cef2SBarry Smith Level: intermediate 907d763cef2SBarry Smith 908d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 909d763cef2SBarry Smith 910d763cef2SBarry Smith .seealso: TSSetApplicationContext() 911d763cef2SBarry Smith @*/ 912e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 913d763cef2SBarry Smith { 914d763cef2SBarry Smith PetscFunctionBegin; 9150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 916e71120c6SJed Brown *(void**)usrP = ts->user; 917d763cef2SBarry Smith PetscFunctionReturn(0); 918d763cef2SBarry Smith } 919d763cef2SBarry Smith 9204a2ae208SSatish Balay #undef __FUNCT__ 9214a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 922d763cef2SBarry Smith /*@ 923d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 924d763cef2SBarry Smith 925d763cef2SBarry Smith Not Collective 926d763cef2SBarry Smith 927d763cef2SBarry Smith Input Parameter: 928d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 929d763cef2SBarry Smith 930d763cef2SBarry Smith Output Parameter: 931d763cef2SBarry Smith . iter - number steps so far 932d763cef2SBarry Smith 933d763cef2SBarry Smith Level: intermediate 934d763cef2SBarry Smith 935d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 936d763cef2SBarry Smith @*/ 9377087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 938d763cef2SBarry Smith { 939d763cef2SBarry Smith PetscFunctionBegin; 9400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9414482741eSBarry Smith PetscValidIntPointer(iter,2); 942d763cef2SBarry Smith *iter = ts->steps; 943d763cef2SBarry Smith PetscFunctionReturn(0); 944d763cef2SBarry Smith } 945d763cef2SBarry Smith 9464a2ae208SSatish Balay #undef __FUNCT__ 9474a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 948d763cef2SBarry Smith /*@ 949d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 950d763cef2SBarry Smith as well as the initial time. 951d763cef2SBarry Smith 9523f9fe445SBarry Smith Logically Collective on TS 953d763cef2SBarry Smith 954d763cef2SBarry Smith Input Parameters: 955d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 956d763cef2SBarry Smith . initial_time - the initial time 957d763cef2SBarry Smith - time_step - the size of the timestep 958d763cef2SBarry Smith 959d763cef2SBarry Smith Level: intermediate 960d763cef2SBarry Smith 961d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 962d763cef2SBarry Smith 963d763cef2SBarry Smith .keywords: TS, set, initial, timestep 964d763cef2SBarry Smith @*/ 9657087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 966d763cef2SBarry Smith { 967e144a568SJed Brown PetscErrorCode ierr; 968e144a568SJed Brown 969d763cef2SBarry Smith PetscFunctionBegin; 9700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 971e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 972d763cef2SBarry Smith ts->initial_time_step = time_step; 973d763cef2SBarry Smith ts->ptime = initial_time; 974d763cef2SBarry Smith PetscFunctionReturn(0); 975d763cef2SBarry Smith } 976d763cef2SBarry Smith 9774a2ae208SSatish Balay #undef __FUNCT__ 9784a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 979d763cef2SBarry Smith /*@ 980d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 981d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 982d763cef2SBarry Smith 9833f9fe445SBarry Smith Logically Collective on TS 984d763cef2SBarry Smith 985d763cef2SBarry Smith Input Parameters: 986d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 987d763cef2SBarry Smith - time_step - the size of the timestep 988d763cef2SBarry Smith 989d763cef2SBarry Smith Level: intermediate 990d763cef2SBarry Smith 991d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 992d763cef2SBarry Smith 993d763cef2SBarry Smith .keywords: TS, set, timestep 994d763cef2SBarry Smith @*/ 9957087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 996d763cef2SBarry Smith { 997d763cef2SBarry Smith PetscFunctionBegin; 9980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 999c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1000d763cef2SBarry Smith ts->time_step = time_step; 1001193ac0bcSJed Brown ts->next_time_step = time_step; 1002d763cef2SBarry Smith PetscFunctionReturn(0); 1003d763cef2SBarry Smith } 1004d763cef2SBarry Smith 10054a2ae208SSatish Balay #undef __FUNCT__ 10064a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1007d763cef2SBarry Smith /*@ 1008d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1009d763cef2SBarry Smith 1010d763cef2SBarry Smith Not Collective 1011d763cef2SBarry Smith 1012d763cef2SBarry Smith Input Parameter: 1013d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1014d763cef2SBarry Smith 1015d763cef2SBarry Smith Output Parameter: 1016d763cef2SBarry Smith . dt - the current timestep size 1017d763cef2SBarry Smith 1018d763cef2SBarry Smith Level: intermediate 1019d763cef2SBarry Smith 1020d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1021d763cef2SBarry Smith 1022d763cef2SBarry Smith .keywords: TS, get, timestep 1023d763cef2SBarry Smith @*/ 10247087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1025d763cef2SBarry Smith { 1026d763cef2SBarry Smith PetscFunctionBegin; 10270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10284482741eSBarry Smith PetscValidDoublePointer(dt,2); 1029d763cef2SBarry Smith *dt = ts->time_step; 1030d763cef2SBarry Smith PetscFunctionReturn(0); 1031d763cef2SBarry Smith } 1032d763cef2SBarry Smith 10334a2ae208SSatish Balay #undef __FUNCT__ 10344a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1035d8e5e3e6SSatish Balay /*@ 1036d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1037d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1038d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1039d763cef2SBarry Smith the solution at the next timestep has been calculated. 1040d763cef2SBarry Smith 1041d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1042d763cef2SBarry Smith 1043d763cef2SBarry Smith Input Parameter: 1044d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1045d763cef2SBarry Smith 1046d763cef2SBarry Smith Output Parameter: 1047d763cef2SBarry Smith . v - the vector containing the solution 1048d763cef2SBarry Smith 1049d763cef2SBarry Smith Level: intermediate 1050d763cef2SBarry Smith 1051d763cef2SBarry Smith .seealso: TSGetTimeStep() 1052d763cef2SBarry Smith 1053d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1054d763cef2SBarry Smith @*/ 10557087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1056d763cef2SBarry Smith { 1057d763cef2SBarry Smith PetscFunctionBegin; 10580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10594482741eSBarry Smith PetscValidPointer(v,2); 10608737fe31SLisandro Dalcin *v = ts->vec_sol; 1061d763cef2SBarry Smith PetscFunctionReturn(0); 1062d763cef2SBarry Smith } 1063d763cef2SBarry Smith 1064bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 10654a2ae208SSatish Balay #undef __FUNCT__ 1066bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1067d8e5e3e6SSatish Balay /*@ 1068bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1069d763cef2SBarry Smith 1070bdad233fSMatthew Knepley Not collective 1071d763cef2SBarry Smith 1072bdad233fSMatthew Knepley Input Parameters: 1073bdad233fSMatthew Knepley + ts - The TS 1074bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1075d763cef2SBarry Smith .vb 1076d763cef2SBarry Smith U_t = A U 1077d763cef2SBarry Smith U_t = A(t) U 1078d763cef2SBarry Smith U_t = F(t,U) 1079d763cef2SBarry Smith .ve 1080d763cef2SBarry Smith 1081d763cef2SBarry Smith Level: beginner 1082d763cef2SBarry Smith 1083bdad233fSMatthew Knepley .keywords: TS, problem type 1084bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1085d763cef2SBarry Smith @*/ 10867087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1087a7cc72afSBarry Smith { 10889e2a6581SJed Brown PetscErrorCode ierr; 10899e2a6581SJed Brown 1090d763cef2SBarry Smith PetscFunctionBegin; 10910700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1092bdad233fSMatthew Knepley ts->problem_type = type; 10939e2a6581SJed Brown if (type == TS_LINEAR) { 10949e2a6581SJed Brown SNES snes; 10959e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10969e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 10979e2a6581SJed Brown } 1098d763cef2SBarry Smith PetscFunctionReturn(0); 1099d763cef2SBarry Smith } 1100d763cef2SBarry Smith 1101bdad233fSMatthew Knepley #undef __FUNCT__ 1102bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1103bdad233fSMatthew Knepley /*@C 1104bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1105bdad233fSMatthew Knepley 1106bdad233fSMatthew Knepley Not collective 1107bdad233fSMatthew Knepley 1108bdad233fSMatthew Knepley Input Parameter: 1109bdad233fSMatthew Knepley . ts - The TS 1110bdad233fSMatthew Knepley 1111bdad233fSMatthew Knepley Output Parameter: 1112bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1113bdad233fSMatthew Knepley .vb 1114089b2837SJed Brown M U_t = A U 1115089b2837SJed Brown M(t) U_t = A(t) U 1116bdad233fSMatthew Knepley U_t = F(t,U) 1117bdad233fSMatthew Knepley .ve 1118bdad233fSMatthew Knepley 1119bdad233fSMatthew Knepley Level: beginner 1120bdad233fSMatthew Knepley 1121bdad233fSMatthew Knepley .keywords: TS, problem type 1122bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1123bdad233fSMatthew Knepley @*/ 11247087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1125a7cc72afSBarry Smith { 1126bdad233fSMatthew Knepley PetscFunctionBegin; 11270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11284482741eSBarry Smith PetscValidIntPointer(type,2); 1129bdad233fSMatthew Knepley *type = ts->problem_type; 1130bdad233fSMatthew Knepley PetscFunctionReturn(0); 1131bdad233fSMatthew Knepley } 1132d763cef2SBarry Smith 11334a2ae208SSatish Balay #undef __FUNCT__ 11344a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1135d763cef2SBarry Smith /*@ 1136d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1137d763cef2SBarry Smith of a timestepper. 1138d763cef2SBarry Smith 1139d763cef2SBarry Smith Collective on TS 1140d763cef2SBarry Smith 1141d763cef2SBarry Smith Input Parameter: 1142d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1143d763cef2SBarry Smith 1144d763cef2SBarry Smith Notes: 1145d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1146d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1147d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1148d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1149d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1150d763cef2SBarry Smith 1151d763cef2SBarry Smith Level: advanced 1152d763cef2SBarry Smith 1153d763cef2SBarry Smith .keywords: TS, timestep, setup 1154d763cef2SBarry Smith 1155d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1156d763cef2SBarry Smith @*/ 11577087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1158d763cef2SBarry Smith { 1159dfbe8321SBarry Smith PetscErrorCode ierr; 1160d763cef2SBarry Smith 1161d763cef2SBarry Smith PetscFunctionBegin; 11620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1163277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1164277b19d0SLisandro Dalcin 11657adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 11669596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1167d763cef2SBarry Smith } 1168277b19d0SLisandro Dalcin 1169277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1170277b19d0SLisandro Dalcin 1171277b19d0SLisandro Dalcin if (ts->ops->setup) { 1172000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1173277b19d0SLisandro Dalcin } 1174277b19d0SLisandro Dalcin 1175277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1176277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1177277b19d0SLisandro Dalcin } 1178277b19d0SLisandro Dalcin 1179277b19d0SLisandro Dalcin #undef __FUNCT__ 1180277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1181277b19d0SLisandro Dalcin /*@ 1182277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1183277b19d0SLisandro Dalcin 1184277b19d0SLisandro Dalcin Collective on TS 1185277b19d0SLisandro Dalcin 1186277b19d0SLisandro Dalcin Input Parameter: 1187277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1188277b19d0SLisandro Dalcin 1189277b19d0SLisandro Dalcin Level: beginner 1190277b19d0SLisandro Dalcin 1191277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1192277b19d0SLisandro Dalcin 1193277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1194277b19d0SLisandro Dalcin @*/ 1195277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1196277b19d0SLisandro Dalcin { 1197277b19d0SLisandro Dalcin PetscErrorCode ierr; 1198277b19d0SLisandro Dalcin 1199277b19d0SLisandro Dalcin PetscFunctionBegin; 1200277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1201277b19d0SLisandro Dalcin if (ts->ops->reset) { 1202277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1203277b19d0SLisandro Dalcin } 1204277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12054e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 12064e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1207214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 12086bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1209277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1210277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1211d763cef2SBarry Smith PetscFunctionReturn(0); 1212d763cef2SBarry Smith } 1213d763cef2SBarry Smith 12144a2ae208SSatish Balay #undef __FUNCT__ 12154a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1216d8e5e3e6SSatish Balay /*@ 1217d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1218d763cef2SBarry Smith with TSCreate(). 1219d763cef2SBarry Smith 1220d763cef2SBarry Smith Collective on TS 1221d763cef2SBarry Smith 1222d763cef2SBarry Smith Input Parameter: 1223d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1224d763cef2SBarry Smith 1225d763cef2SBarry Smith Level: beginner 1226d763cef2SBarry Smith 1227d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1228d763cef2SBarry Smith 1229d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1230d763cef2SBarry Smith @*/ 12316bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1232d763cef2SBarry Smith { 12336849ba73SBarry Smith PetscErrorCode ierr; 1234d763cef2SBarry Smith 1235d763cef2SBarry Smith PetscFunctionBegin; 12366bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12376bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12386bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1239d763cef2SBarry Smith 12406bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1241277b19d0SLisandro Dalcin 1242be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12436bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 12446bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 12456d4c513bSLisandro Dalcin 12466bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 12476bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 12486bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 12496d4c513bSLisandro Dalcin 1250a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1251d763cef2SBarry Smith PetscFunctionReturn(0); 1252d763cef2SBarry Smith } 1253d763cef2SBarry Smith 12544a2ae208SSatish Balay #undef __FUNCT__ 12554a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1256d8e5e3e6SSatish Balay /*@ 1257d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1258d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1259d763cef2SBarry Smith 1260d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1261d763cef2SBarry Smith 1262d763cef2SBarry Smith Input Parameter: 1263d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1264d763cef2SBarry Smith 1265d763cef2SBarry Smith Output Parameter: 1266d763cef2SBarry Smith . snes - the nonlinear solver context 1267d763cef2SBarry Smith 1268d763cef2SBarry Smith Notes: 1269d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1270d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 127194b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1272d763cef2SBarry Smith 1273d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1274d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1275d763cef2SBarry Smith 1276d763cef2SBarry Smith Level: beginner 1277d763cef2SBarry Smith 1278d763cef2SBarry Smith .keywords: timestep, get, SNES 1279d763cef2SBarry Smith @*/ 12807087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1281d763cef2SBarry Smith { 1282d372ba47SLisandro Dalcin PetscErrorCode ierr; 1283d372ba47SLisandro Dalcin 1284d763cef2SBarry Smith PetscFunctionBegin; 12850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12864482741eSBarry Smith PetscValidPointer(snes,2); 1287d372ba47SLisandro Dalcin if (!ts->snes) { 1288d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1289d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1290d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 12919e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 12929e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 12939e2a6581SJed Brown } 1294d372ba47SLisandro Dalcin } 1295d763cef2SBarry Smith *snes = ts->snes; 1296d763cef2SBarry Smith PetscFunctionReturn(0); 1297d763cef2SBarry Smith } 1298d763cef2SBarry Smith 12994a2ae208SSatish Balay #undef __FUNCT__ 130094b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1301d8e5e3e6SSatish Balay /*@ 130294b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1303d763cef2SBarry Smith a TS (timestepper) context. 1304d763cef2SBarry Smith 130594b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1306d763cef2SBarry Smith 1307d763cef2SBarry Smith Input Parameter: 1308d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1309d763cef2SBarry Smith 1310d763cef2SBarry Smith Output Parameter: 131194b7f48cSBarry Smith . ksp - the nonlinear solver context 1312d763cef2SBarry Smith 1313d763cef2SBarry Smith Notes: 131494b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1315d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1316d763cef2SBarry Smith KSP and PC contexts as well. 1317d763cef2SBarry Smith 131894b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 131994b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1320d763cef2SBarry Smith 1321d763cef2SBarry Smith Level: beginner 1322d763cef2SBarry Smith 132394b7f48cSBarry Smith .keywords: timestep, get, KSP 1324d763cef2SBarry Smith @*/ 13257087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1326d763cef2SBarry Smith { 1327d372ba47SLisandro Dalcin PetscErrorCode ierr; 1328089b2837SJed Brown SNES snes; 1329d372ba47SLisandro Dalcin 1330d763cef2SBarry Smith PetscFunctionBegin; 13310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13324482741eSBarry Smith PetscValidPointer(ksp,2); 133317186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1334e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1335089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1336089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1337d763cef2SBarry Smith PetscFunctionReturn(0); 1338d763cef2SBarry Smith } 1339d763cef2SBarry Smith 1340d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1341d763cef2SBarry Smith 13424a2ae208SSatish Balay #undef __FUNCT__ 1343adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1344adb62b0dSMatthew Knepley /*@ 1345adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1346adb62b0dSMatthew Knepley maximum time for iteration. 1347adb62b0dSMatthew Knepley 13483f9fe445SBarry Smith Not Collective 1349adb62b0dSMatthew Knepley 1350adb62b0dSMatthew Knepley Input Parameters: 1351adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1352adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1353adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1354adb62b0dSMatthew Knepley 1355adb62b0dSMatthew Knepley Level: intermediate 1356adb62b0dSMatthew Knepley 1357adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1358adb62b0dSMatthew Knepley @*/ 13597087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1360adb62b0dSMatthew Knepley { 1361adb62b0dSMatthew Knepley PetscFunctionBegin; 13620700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1363abc0a331SBarry Smith if (maxsteps) { 13644482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1365adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1366adb62b0dSMatthew Knepley } 1367abc0a331SBarry Smith if (maxtime ) { 13684482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1369adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1370adb62b0dSMatthew Knepley } 1371adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1372adb62b0dSMatthew Knepley } 1373adb62b0dSMatthew Knepley 1374adb62b0dSMatthew Knepley #undef __FUNCT__ 13754a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1376d763cef2SBarry Smith /*@ 1377d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1378d763cef2SBarry Smith maximum time for iteration. 1379d763cef2SBarry Smith 13803f9fe445SBarry Smith Logically Collective on TS 1381d763cef2SBarry Smith 1382d763cef2SBarry Smith Input Parameters: 1383d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1384d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1385d763cef2SBarry Smith - maxtime - final time to iterate to 1386d763cef2SBarry Smith 1387d763cef2SBarry Smith Options Database Keys: 1388d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1389d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1390d763cef2SBarry Smith 1391d763cef2SBarry Smith Notes: 1392d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1393d763cef2SBarry Smith 1394d763cef2SBarry Smith Level: intermediate 1395d763cef2SBarry Smith 1396d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1397d763cef2SBarry Smith @*/ 13987087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1399d763cef2SBarry Smith { 1400d763cef2SBarry Smith PetscFunctionBegin; 14010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1402c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1403c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1404d763cef2SBarry Smith ts->max_steps = maxsteps; 1405d763cef2SBarry Smith ts->max_time = maxtime; 1406d763cef2SBarry Smith PetscFunctionReturn(0); 1407d763cef2SBarry Smith } 1408d763cef2SBarry Smith 14094a2ae208SSatish Balay #undef __FUNCT__ 14104a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1411d763cef2SBarry Smith /*@ 1412d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1413d763cef2SBarry Smith for use by the TS routines. 1414d763cef2SBarry Smith 14153f9fe445SBarry Smith Logically Collective on TS and Vec 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith Input Parameters: 1418d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1419d763cef2SBarry Smith - x - the solution vector 1420d763cef2SBarry Smith 1421d763cef2SBarry Smith Level: beginner 1422d763cef2SBarry Smith 1423d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1424d763cef2SBarry Smith @*/ 14257087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1426d763cef2SBarry Smith { 14278737fe31SLisandro Dalcin PetscErrorCode ierr; 14288737fe31SLisandro Dalcin 1429d763cef2SBarry Smith PetscFunctionBegin; 14300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14310700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14328737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14336bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14348737fe31SLisandro Dalcin ts->vec_sol = x; 1435d763cef2SBarry Smith PetscFunctionReturn(0); 1436d763cef2SBarry Smith } 1437d763cef2SBarry Smith 1438e74ef692SMatthew Knepley #undef __FUNCT__ 1439e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1440ac226902SBarry Smith /*@C 1441000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14423f2090d5SJed Brown called once at the beginning of each time step. 1443000e7ae3SMatthew Knepley 14443f9fe445SBarry Smith Logically Collective on TS 1445000e7ae3SMatthew Knepley 1446000e7ae3SMatthew Knepley Input Parameters: 1447000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1448000e7ae3SMatthew Knepley - func - The function 1449000e7ae3SMatthew Knepley 1450000e7ae3SMatthew Knepley Calling sequence of func: 1451000e7ae3SMatthew Knepley . func (TS ts); 1452000e7ae3SMatthew Knepley 1453000e7ae3SMatthew Knepley Level: intermediate 1454000e7ae3SMatthew Knepley 1455000e7ae3SMatthew Knepley .keywords: TS, timestep 1456000e7ae3SMatthew Knepley @*/ 14577087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1458000e7ae3SMatthew Knepley { 1459000e7ae3SMatthew Knepley PetscFunctionBegin; 14600700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1461000e7ae3SMatthew Knepley ts->ops->prestep = func; 1462000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1463000e7ae3SMatthew Knepley } 1464000e7ae3SMatthew Knepley 1465e74ef692SMatthew Knepley #undef __FUNCT__ 14663f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 14673f2090d5SJed Brown /*@C 14683f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 14693f2090d5SJed Brown 14703f2090d5SJed Brown Collective on TS 14713f2090d5SJed Brown 14723f2090d5SJed Brown Input Parameters: 14733f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 14743f2090d5SJed Brown 14753f2090d5SJed Brown Notes: 14763f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 14773f2090d5SJed Brown so most users would not generally call this routine themselves. 14783f2090d5SJed Brown 14793f2090d5SJed Brown Level: developer 14803f2090d5SJed Brown 14813f2090d5SJed Brown .keywords: TS, timestep 14823f2090d5SJed Brown @*/ 14837087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 14843f2090d5SJed Brown { 14853f2090d5SJed Brown PetscErrorCode ierr; 14863f2090d5SJed Brown 14873f2090d5SJed Brown PetscFunctionBegin; 14880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 148972ac3e02SJed Brown if (ts->ops->prestep) { 14903f2090d5SJed Brown PetscStackPush("TS PreStep function"); 14913f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 14923f2090d5SJed Brown PetscStackPop; 1493312ce896SJed Brown } 14943f2090d5SJed Brown PetscFunctionReturn(0); 14953f2090d5SJed Brown } 14963f2090d5SJed Brown 14973f2090d5SJed Brown #undef __FUNCT__ 1498e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1499ac226902SBarry Smith /*@C 1500000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15013f2090d5SJed Brown called once at the end of each time step. 1502000e7ae3SMatthew Knepley 15033f9fe445SBarry Smith Logically Collective on TS 1504000e7ae3SMatthew Knepley 1505000e7ae3SMatthew Knepley Input Parameters: 1506000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1507000e7ae3SMatthew Knepley - func - The function 1508000e7ae3SMatthew Knepley 1509000e7ae3SMatthew Knepley Calling sequence of func: 1510000e7ae3SMatthew Knepley . func (TS ts); 1511000e7ae3SMatthew Knepley 1512000e7ae3SMatthew Knepley Level: intermediate 1513000e7ae3SMatthew Knepley 1514000e7ae3SMatthew Knepley .keywords: TS, timestep 1515000e7ae3SMatthew Knepley @*/ 15167087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1517000e7ae3SMatthew Knepley { 1518000e7ae3SMatthew Knepley PetscFunctionBegin; 15190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1520000e7ae3SMatthew Knepley ts->ops->poststep = func; 1521000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1522000e7ae3SMatthew Knepley } 1523000e7ae3SMatthew Knepley 1524e74ef692SMatthew Knepley #undef __FUNCT__ 15253f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15263f2090d5SJed Brown /*@C 15273f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15283f2090d5SJed Brown 15293f2090d5SJed Brown Collective on TS 15303f2090d5SJed Brown 15313f2090d5SJed Brown Input Parameters: 15323f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15333f2090d5SJed Brown 15343f2090d5SJed Brown Notes: 15353f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15363f2090d5SJed Brown so most users would not generally call this routine themselves. 15373f2090d5SJed Brown 15383f2090d5SJed Brown Level: developer 15393f2090d5SJed Brown 15403f2090d5SJed Brown .keywords: TS, timestep 15413f2090d5SJed Brown @*/ 15427087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15433f2090d5SJed Brown { 15443f2090d5SJed Brown PetscErrorCode ierr; 15453f2090d5SJed Brown 15463f2090d5SJed Brown PetscFunctionBegin; 15470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 154872ac3e02SJed Brown if (ts->ops->poststep) { 15493f2090d5SJed Brown PetscStackPush("TS PostStep function"); 15503f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 15513f2090d5SJed Brown PetscStackPop; 155272ac3e02SJed Brown } 15533f2090d5SJed Brown PetscFunctionReturn(0); 15543f2090d5SJed Brown } 15553f2090d5SJed Brown 1556d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1557d763cef2SBarry Smith 15584a2ae208SSatish Balay #undef __FUNCT__ 1559a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1560d763cef2SBarry Smith /*@C 1561a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1562d763cef2SBarry Smith timestep to display the iteration's progress. 1563d763cef2SBarry Smith 15643f9fe445SBarry Smith Logically Collective on TS 1565d763cef2SBarry Smith 1566d763cef2SBarry Smith Input Parameters: 1567d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1568d763cef2SBarry Smith . func - monitoring routine 1569329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1570b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1571b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1572b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1573d763cef2SBarry Smith 1574d763cef2SBarry Smith Calling sequence of func: 1575a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1576d763cef2SBarry Smith 1577d763cef2SBarry Smith + ts - the TS context 1578d763cef2SBarry Smith . steps - iteration number 15791f06c33eSBarry Smith . time - current time 1580d763cef2SBarry Smith . x - current iterate 1581d763cef2SBarry Smith - mctx - [optional] monitoring context 1582d763cef2SBarry Smith 1583d763cef2SBarry Smith Notes: 1584d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1585d763cef2SBarry Smith already has been loaded. 1586d763cef2SBarry Smith 1587025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1588025f1a04SBarry Smith 1589d763cef2SBarry Smith Level: intermediate 1590d763cef2SBarry Smith 1591d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1592d763cef2SBarry Smith 1593a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1594d763cef2SBarry Smith @*/ 1595c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1596d763cef2SBarry Smith { 1597d763cef2SBarry Smith PetscFunctionBegin; 15980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 159917186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1600d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1601329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1602d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1603d763cef2SBarry Smith PetscFunctionReturn(0); 1604d763cef2SBarry Smith } 1605d763cef2SBarry Smith 16064a2ae208SSatish Balay #undef __FUNCT__ 1607a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1608d763cef2SBarry Smith /*@C 1609a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1610d763cef2SBarry Smith 16113f9fe445SBarry Smith Logically Collective on TS 1612d763cef2SBarry Smith 1613d763cef2SBarry Smith Input Parameters: 1614d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1615d763cef2SBarry Smith 1616d763cef2SBarry Smith Notes: 1617d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1618d763cef2SBarry Smith 1619d763cef2SBarry Smith Level: intermediate 1620d763cef2SBarry Smith 1621d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1622d763cef2SBarry Smith 1623a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1624d763cef2SBarry Smith @*/ 16257087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1626d763cef2SBarry Smith { 1627d952e501SBarry Smith PetscErrorCode ierr; 1628d952e501SBarry Smith PetscInt i; 1629d952e501SBarry Smith 1630d763cef2SBarry Smith PetscFunctionBegin; 16310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1632d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1633d952e501SBarry Smith if (ts->mdestroy[i]) { 16343c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1635d952e501SBarry Smith } 1636d952e501SBarry Smith } 1637d763cef2SBarry Smith ts->numbermonitors = 0; 1638d763cef2SBarry Smith PetscFunctionReturn(0); 1639d763cef2SBarry Smith } 1640d763cef2SBarry Smith 16414a2ae208SSatish Balay #undef __FUNCT__ 1642a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1643d8e5e3e6SSatish Balay /*@ 1644a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16455516499fSSatish Balay 16465516499fSSatish Balay Level: intermediate 164741251cbbSSatish Balay 16485516499fSSatish Balay .keywords: TS, set, monitor 16495516499fSSatish Balay 165041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 165141251cbbSSatish Balay @*/ 1652649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1653d763cef2SBarry Smith { 1654dfbe8321SBarry Smith PetscErrorCode ierr; 1655649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1656d132466eSBarry Smith 1657d763cef2SBarry Smith PetscFunctionBegin; 1658649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1659649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1660649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1661d763cef2SBarry Smith PetscFunctionReturn(0); 1662d763cef2SBarry Smith } 1663d763cef2SBarry Smith 16644a2ae208SSatish Balay #undef __FUNCT__ 16654a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1666d763cef2SBarry Smith /*@ 1667d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1668d763cef2SBarry Smith 1669d763cef2SBarry Smith Collective on TS 1670d763cef2SBarry Smith 1671d763cef2SBarry Smith Input Parameter: 1672d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1673d763cef2SBarry Smith 1674d763cef2SBarry Smith Output Parameters: 1675d763cef2SBarry Smith + steps - number of iterations until termination 1676142b95e3SSatish Balay - ptime - time until termination 1677d763cef2SBarry Smith 1678d763cef2SBarry Smith Level: beginner 1679d763cef2SBarry Smith 1680d763cef2SBarry Smith .keywords: TS, timestep, solve 1681d763cef2SBarry Smith 1682d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1683d763cef2SBarry Smith @*/ 1684193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1685d763cef2SBarry Smith { 1686dfbe8321SBarry Smith PetscErrorCode ierr; 1687d763cef2SBarry Smith 1688d763cef2SBarry Smith PetscFunctionBegin; 16890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1690277b19d0SLisandro Dalcin 1691d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1692d405a339SMatthew Knepley 1693d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1694193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1695d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1696d763cef2SBarry Smith PetscFunctionReturn(0); 1697d763cef2SBarry Smith } 1698d763cef2SBarry Smith 16994a2ae208SSatish Balay #undef __FUNCT__ 17006a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17016a4d4014SLisandro Dalcin /*@ 17026a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17036a4d4014SLisandro Dalcin 17046a4d4014SLisandro Dalcin Collective on TS 17056a4d4014SLisandro Dalcin 17066a4d4014SLisandro Dalcin Input Parameter: 17076a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17086a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17096a4d4014SLisandro Dalcin 17106a4d4014SLisandro Dalcin Level: beginner 17116a4d4014SLisandro Dalcin 17126a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17136a4d4014SLisandro Dalcin 17146a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17156a4d4014SLisandro Dalcin @*/ 17167087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17176a4d4014SLisandro Dalcin { 1718193ac0bcSJed Brown PetscInt i; 17196a4d4014SLisandro Dalcin PetscErrorCode ierr; 1720f22f69f0SBarry Smith 17216a4d4014SLisandro Dalcin PetscFunctionBegin; 17220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1723193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1724193ac0bcSJed Brown ierr = TSSetSolution(ts,x); CHKERRQ(ierr); 17256a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1726193ac0bcSJed Brown ts->steps = 0; 1727193ac0bcSJed Brown ts->linear_its = 0; 1728193ac0bcSJed Brown ts->nonlinear_its = 0; 1729193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1730193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1731193ac0bcSJed Brown 1732193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1733193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 1734193ac0bcSJed Brown } else { 17356a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 17363daf2de8SJed Brown for (i=0; !ts->reason; ) { 1737193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1738193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1739193ac0bcSJed Brown if (ts->reason < 0) { 1740193ac0bcSJed Brown if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed"); 17413daf2de8SJed Brown } else if (++i >= ts->max_steps) { 1742193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITS; 1743193ac0bcSJed Brown } else if (ts->ptime >= ts->max_time) { 1744193ac0bcSJed Brown ts->reason = TS_CONVERGED_TIME; 1745193ac0bcSJed Brown } 1746193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1747193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1748193ac0bcSJed Brown } 1749193ac0bcSJed Brown } 1750193ac0bcSJed Brown if (!PetscPreLoadingOn) { 1751193ac0bcSJed Brown ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1752193ac0bcSJed Brown } 17536a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17546a4d4014SLisandro Dalcin } 17556a4d4014SLisandro Dalcin 17566a4d4014SLisandro Dalcin #undef __FUNCT__ 17574a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1758d763cef2SBarry Smith /* 1759d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1760d763cef2SBarry Smith */ 1761a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1762d763cef2SBarry Smith { 17636849ba73SBarry Smith PetscErrorCode ierr; 1764a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1765d763cef2SBarry Smith 1766d763cef2SBarry Smith PetscFunctionBegin; 1767d763cef2SBarry Smith for (i=0; i<n; i++) { 1768142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1769d763cef2SBarry Smith } 1770d763cef2SBarry Smith PetscFunctionReturn(0); 1771d763cef2SBarry Smith } 1772d763cef2SBarry Smith 1773d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1774d763cef2SBarry Smith 17754a2ae208SSatish Balay #undef __FUNCT__ 1776a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1777d763cef2SBarry Smith /*@C 1778a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1779d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1780d763cef2SBarry Smith 1781d763cef2SBarry Smith Collective on TS 1782d763cef2SBarry Smith 1783d763cef2SBarry Smith Input Parameters: 1784d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1785d763cef2SBarry Smith . label - the title to put in the title bar 17867c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1787d763cef2SBarry Smith - m, n - the screen width and height in pixels 1788d763cef2SBarry Smith 1789d763cef2SBarry Smith Output Parameter: 1790d763cef2SBarry Smith . draw - the drawing context 1791d763cef2SBarry Smith 1792d763cef2SBarry Smith Options Database Key: 1793a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1794d763cef2SBarry Smith 1795d763cef2SBarry Smith Notes: 1796a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1797d763cef2SBarry Smith 1798d763cef2SBarry Smith Level: intermediate 1799d763cef2SBarry Smith 18007c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1801d763cef2SBarry Smith 1802a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18037c922b88SBarry Smith 1804d763cef2SBarry Smith @*/ 18057087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1806d763cef2SBarry Smith { 1807b0a32e0cSBarry Smith PetscDraw win; 1808dfbe8321SBarry Smith PetscErrorCode ierr; 1809d763cef2SBarry Smith 1810d763cef2SBarry Smith PetscFunctionBegin; 1811b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1812b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1813b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1814b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1815d763cef2SBarry Smith 181652e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1817d763cef2SBarry Smith PetscFunctionReturn(0); 1818d763cef2SBarry Smith } 1819d763cef2SBarry Smith 18204a2ae208SSatish Balay #undef __FUNCT__ 1821a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1822a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1823d763cef2SBarry Smith { 1824b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 182587828ca2SBarry Smith PetscReal x,y = ptime; 1826dfbe8321SBarry Smith PetscErrorCode ierr; 1827d763cef2SBarry Smith 1828d763cef2SBarry Smith PetscFunctionBegin; 18297c922b88SBarry Smith if (!monctx) { 18307c922b88SBarry Smith MPI_Comm comm; 1831b0a32e0cSBarry Smith PetscViewer viewer; 18327c922b88SBarry Smith 18337c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1834b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1835b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18367c922b88SBarry Smith } 18377c922b88SBarry Smith 1838b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 183987828ca2SBarry Smith x = (PetscReal)n; 1840b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1841d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1842b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1843d763cef2SBarry Smith } 1844d763cef2SBarry Smith PetscFunctionReturn(0); 1845d763cef2SBarry Smith } 1846d763cef2SBarry Smith 18474a2ae208SSatish Balay #undef __FUNCT__ 1848a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1849d763cef2SBarry Smith /*@C 1850a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1851a6570f20SBarry Smith with TSMonitorLGCreate(). 1852d763cef2SBarry Smith 1853b0a32e0cSBarry Smith Collective on PetscDrawLG 1854d763cef2SBarry Smith 1855d763cef2SBarry Smith Input Parameter: 1856d763cef2SBarry Smith . draw - the drawing context 1857d763cef2SBarry Smith 1858d763cef2SBarry Smith Level: intermediate 1859d763cef2SBarry Smith 1860d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1861d763cef2SBarry Smith 1862a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1863d763cef2SBarry Smith @*/ 18646bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1865d763cef2SBarry Smith { 1866b0a32e0cSBarry Smith PetscDraw draw; 1867dfbe8321SBarry Smith PetscErrorCode ierr; 1868d763cef2SBarry Smith 1869d763cef2SBarry Smith PetscFunctionBegin; 18706bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 18716bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1872b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1873d763cef2SBarry Smith PetscFunctionReturn(0); 1874d763cef2SBarry Smith } 1875d763cef2SBarry Smith 18764a2ae208SSatish Balay #undef __FUNCT__ 18774a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1878d763cef2SBarry Smith /*@ 1879d763cef2SBarry Smith TSGetTime - Gets the current time. 1880d763cef2SBarry Smith 1881d763cef2SBarry Smith Not Collective 1882d763cef2SBarry Smith 1883d763cef2SBarry Smith Input Parameter: 1884d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1885d763cef2SBarry Smith 1886d763cef2SBarry Smith Output Parameter: 1887d763cef2SBarry Smith . t - the current time 1888d763cef2SBarry Smith 1889d763cef2SBarry Smith Level: beginner 1890d763cef2SBarry Smith 1891d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1892d763cef2SBarry Smith 1893d763cef2SBarry Smith .keywords: TS, get, time 1894d763cef2SBarry Smith @*/ 18957087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1896d763cef2SBarry Smith { 1897d763cef2SBarry Smith PetscFunctionBegin; 18980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18994482741eSBarry Smith PetscValidDoublePointer(t,2); 1900d763cef2SBarry Smith *t = ts->ptime; 1901d763cef2SBarry Smith PetscFunctionReturn(0); 1902d763cef2SBarry Smith } 1903d763cef2SBarry Smith 19044a2ae208SSatish Balay #undef __FUNCT__ 19056a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19066a4d4014SLisandro Dalcin /*@ 19076a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19086a4d4014SLisandro Dalcin 19093f9fe445SBarry Smith Logically Collective on TS 19106a4d4014SLisandro Dalcin 19116a4d4014SLisandro Dalcin Input Parameters: 19126a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19136a4d4014SLisandro Dalcin - time - the time 19146a4d4014SLisandro Dalcin 19156a4d4014SLisandro Dalcin Level: intermediate 19166a4d4014SLisandro Dalcin 19176a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19186a4d4014SLisandro Dalcin 19196a4d4014SLisandro Dalcin .keywords: TS, set, time 19206a4d4014SLisandro Dalcin @*/ 19217087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19226a4d4014SLisandro Dalcin { 19236a4d4014SLisandro Dalcin PetscFunctionBegin; 19240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1925c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19266a4d4014SLisandro Dalcin ts->ptime = t; 19276a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19286a4d4014SLisandro Dalcin } 19296a4d4014SLisandro Dalcin 19306a4d4014SLisandro Dalcin #undef __FUNCT__ 19314a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1932d763cef2SBarry Smith /*@C 1933d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1934d763cef2SBarry Smith TS options in the database. 1935d763cef2SBarry Smith 19363f9fe445SBarry Smith Logically Collective on TS 1937d763cef2SBarry Smith 1938d763cef2SBarry Smith Input Parameter: 1939d763cef2SBarry Smith + ts - The TS context 1940d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1941d763cef2SBarry Smith 1942d763cef2SBarry Smith Notes: 1943d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1944d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1945d763cef2SBarry Smith hyphen. 1946d763cef2SBarry Smith 1947d763cef2SBarry Smith Level: advanced 1948d763cef2SBarry Smith 1949d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1950d763cef2SBarry Smith 1951d763cef2SBarry Smith .seealso: TSSetFromOptions() 1952d763cef2SBarry Smith 1953d763cef2SBarry Smith @*/ 19547087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1955d763cef2SBarry Smith { 1956dfbe8321SBarry Smith PetscErrorCode ierr; 1957089b2837SJed Brown SNES snes; 1958d763cef2SBarry Smith 1959d763cef2SBarry Smith PetscFunctionBegin; 19600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1961d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1962089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1963089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 1964d763cef2SBarry Smith PetscFunctionReturn(0); 1965d763cef2SBarry Smith } 1966d763cef2SBarry Smith 1967d763cef2SBarry Smith 19684a2ae208SSatish Balay #undef __FUNCT__ 19694a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 1970d763cef2SBarry Smith /*@C 1971d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 1972d763cef2SBarry Smith TS options in the database. 1973d763cef2SBarry Smith 19743f9fe445SBarry Smith Logically Collective on TS 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith Input Parameter: 1977d763cef2SBarry Smith + ts - The TS context 1978d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1979d763cef2SBarry Smith 1980d763cef2SBarry Smith Notes: 1981d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1982d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1983d763cef2SBarry Smith hyphen. 1984d763cef2SBarry Smith 1985d763cef2SBarry Smith Level: advanced 1986d763cef2SBarry Smith 1987d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 1988d763cef2SBarry Smith 1989d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith @*/ 19927087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 1993d763cef2SBarry Smith { 1994dfbe8321SBarry Smith PetscErrorCode ierr; 1995089b2837SJed Brown SNES snes; 1996d763cef2SBarry Smith 1997d763cef2SBarry Smith PetscFunctionBegin; 19980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1999d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2000089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2001089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2002d763cef2SBarry Smith PetscFunctionReturn(0); 2003d763cef2SBarry Smith } 2004d763cef2SBarry Smith 20054a2ae208SSatish Balay #undef __FUNCT__ 20064a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2007d763cef2SBarry Smith /*@C 2008d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2009d763cef2SBarry Smith TS options in the database. 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith Not Collective 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith Input Parameter: 2014d763cef2SBarry Smith . ts - The TS context 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith Output Parameter: 2017d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2018d763cef2SBarry Smith 2019d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2020d763cef2SBarry Smith sufficient length to hold the prefix. 2021d763cef2SBarry Smith 2022d763cef2SBarry Smith Level: intermediate 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2025d763cef2SBarry Smith 2026d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2027d763cef2SBarry Smith @*/ 20287087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2029d763cef2SBarry Smith { 2030dfbe8321SBarry Smith PetscErrorCode ierr; 2031d763cef2SBarry Smith 2032d763cef2SBarry Smith PetscFunctionBegin; 20330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20344482741eSBarry Smith PetscValidPointer(prefix,2); 2035d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2036d763cef2SBarry Smith PetscFunctionReturn(0); 2037d763cef2SBarry Smith } 2038d763cef2SBarry Smith 20394a2ae208SSatish Balay #undef __FUNCT__ 20404a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2041d763cef2SBarry Smith /*@C 2042d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2043d763cef2SBarry Smith 2044d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2045d763cef2SBarry Smith 2046d763cef2SBarry Smith Input Parameter: 2047d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2048d763cef2SBarry Smith 2049d763cef2SBarry Smith Output Parameters: 2050d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2051d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2052089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2053d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith Level: intermediate 2058d763cef2SBarry Smith 205926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2060d763cef2SBarry Smith 2061d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2062d763cef2SBarry Smith @*/ 2063089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2064d763cef2SBarry Smith { 2065089b2837SJed Brown PetscErrorCode ierr; 2066089b2837SJed Brown SNES snes; 2067089b2837SJed Brown 2068d763cef2SBarry Smith PetscFunctionBegin; 2069089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2070089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2071089b2837SJed Brown if (func) *func = ts->ops->rhsjacobian; 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 { 2102089b2837SJed Brown PetscErrorCode ierr; 2103089b2837SJed Brown SNES snes; 2104089b2837SJed Brown 21052eca1d9cSJed Brown PetscFunctionBegin; 2106089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2107089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21082eca1d9cSJed Brown if (f) *f = ts->ops->ijacobian; 21092eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21102eca1d9cSJed Brown PetscFunctionReturn(0); 21112eca1d9cSJed Brown } 21122eca1d9cSJed Brown 21132eca1d9cSJed Brown #undef __FUNCT__ 2114a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21151713a123SBarry Smith /*@C 2116a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21171713a123SBarry Smith VecView() for the solution at each timestep 21181713a123SBarry Smith 21191713a123SBarry Smith Collective on TS 21201713a123SBarry Smith 21211713a123SBarry Smith Input Parameters: 21221713a123SBarry Smith + ts - the TS context 21231713a123SBarry Smith . step - current time-step 2124142b95e3SSatish Balay . ptime - current time 21251713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21261713a123SBarry Smith 21271713a123SBarry Smith Level: intermediate 21281713a123SBarry Smith 21291713a123SBarry Smith .keywords: TS, vector, monitor, view 21301713a123SBarry Smith 2131a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21321713a123SBarry Smith @*/ 21337087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21341713a123SBarry Smith { 2135dfbe8321SBarry Smith PetscErrorCode ierr; 21361713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21371713a123SBarry Smith 21381713a123SBarry Smith PetscFunctionBegin; 2139a34d58ebSBarry Smith if (!dummy) { 21407adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21411713a123SBarry Smith } 21421713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21431713a123SBarry Smith PetscFunctionReturn(0); 21441713a123SBarry Smith } 21451713a123SBarry Smith 21461713a123SBarry Smith 21476c699258SBarry Smith #undef __FUNCT__ 21486c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21496c699258SBarry Smith /*@ 21506c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21516c699258SBarry Smith 21523f9fe445SBarry Smith Logically Collective on TS and DM 21536c699258SBarry Smith 21546c699258SBarry Smith Input Parameters: 21556c699258SBarry Smith + ts - the preconditioner context 21566c699258SBarry Smith - dm - the dm 21576c699258SBarry Smith 21586c699258SBarry Smith Level: intermediate 21596c699258SBarry Smith 21606c699258SBarry Smith 21616c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 21626c699258SBarry Smith @*/ 21637087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 21646c699258SBarry Smith { 21656c699258SBarry Smith PetscErrorCode ierr; 2166089b2837SJed Brown SNES snes; 21676c699258SBarry Smith 21686c699258SBarry Smith PetscFunctionBegin; 21690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 217070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 21716bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 21726c699258SBarry Smith ts->dm = dm; 2173089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2174089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 21756c699258SBarry Smith PetscFunctionReturn(0); 21766c699258SBarry Smith } 21776c699258SBarry Smith 21786c699258SBarry Smith #undef __FUNCT__ 21796c699258SBarry Smith #define __FUNCT__ "TSGetDM" 21806c699258SBarry Smith /*@ 21816c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 21826c699258SBarry Smith 21833f9fe445SBarry Smith Not Collective 21846c699258SBarry Smith 21856c699258SBarry Smith Input Parameter: 21866c699258SBarry Smith . ts - the preconditioner context 21876c699258SBarry Smith 21886c699258SBarry Smith Output Parameter: 21896c699258SBarry Smith . dm - the dm 21906c699258SBarry Smith 21916c699258SBarry Smith Level: intermediate 21926c699258SBarry Smith 21936c699258SBarry Smith 21946c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 21956c699258SBarry Smith @*/ 21967087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 21976c699258SBarry Smith { 21986c699258SBarry Smith PetscFunctionBegin; 21990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22006c699258SBarry Smith *dm = ts->dm; 22016c699258SBarry Smith PetscFunctionReturn(0); 22026c699258SBarry Smith } 22031713a123SBarry Smith 22040f5c6efeSJed Brown #undef __FUNCT__ 22050f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22060f5c6efeSJed Brown /*@ 22070f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22080f5c6efeSJed Brown 22093f9fe445SBarry Smith Logically Collective on SNES 22100f5c6efeSJed Brown 22110f5c6efeSJed Brown Input Parameter: 2212d42a1c89SJed Brown + snes - nonlinear solver 22130f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2214d42a1c89SJed Brown - ctx - user context, must be a TS 22150f5c6efeSJed Brown 22160f5c6efeSJed Brown Output Parameter: 22170f5c6efeSJed Brown . F - the nonlinear residual 22180f5c6efeSJed Brown 22190f5c6efeSJed Brown Notes: 22200f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22210f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22220f5c6efeSJed Brown 22230f5c6efeSJed Brown Level: advanced 22240f5c6efeSJed Brown 22250f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22260f5c6efeSJed Brown @*/ 22277087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22280f5c6efeSJed Brown { 22290f5c6efeSJed Brown TS ts = (TS)ctx; 22300f5c6efeSJed Brown PetscErrorCode ierr; 22310f5c6efeSJed Brown 22320f5c6efeSJed Brown PetscFunctionBegin; 22330f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22340f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22350f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22360f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22370f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22380f5c6efeSJed Brown PetscFunctionReturn(0); 22390f5c6efeSJed Brown } 22400f5c6efeSJed Brown 22410f5c6efeSJed Brown #undef __FUNCT__ 22420f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22430f5c6efeSJed Brown /*@ 22440f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22450f5c6efeSJed Brown 22460f5c6efeSJed Brown Collective on SNES 22470f5c6efeSJed Brown 22480f5c6efeSJed Brown Input Parameter: 22490f5c6efeSJed Brown + snes - nonlinear solver 22500f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22510f5c6efeSJed Brown - ctx - user context, must be a TS 22520f5c6efeSJed Brown 22530f5c6efeSJed Brown Output Parameter: 22540f5c6efeSJed Brown + A - the Jacobian 22550f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 22560f5c6efeSJed Brown - flag - indicates any structure change in the matrix 22570f5c6efeSJed Brown 22580f5c6efeSJed Brown Notes: 22590f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22600f5c6efeSJed Brown 22610f5c6efeSJed Brown Level: developer 22620f5c6efeSJed Brown 22630f5c6efeSJed Brown .seealso: SNESSetJacobian() 22640f5c6efeSJed Brown @*/ 22657087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 22660f5c6efeSJed Brown { 22670f5c6efeSJed Brown TS ts = (TS)ctx; 22680f5c6efeSJed Brown PetscErrorCode ierr; 22690f5c6efeSJed Brown 22700f5c6efeSJed Brown PetscFunctionBegin; 22710f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22720f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22730f5c6efeSJed Brown PetscValidPointer(A,3); 22740f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 22750f5c6efeSJed Brown PetscValidPointer(B,4); 22760f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 22770f5c6efeSJed Brown PetscValidPointer(flag,5); 22780f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 22790f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 22800f5c6efeSJed Brown PetscFunctionReturn(0); 22810f5c6efeSJed Brown } 2282325fc9f4SBarry Smith 2283*0e4ef248SJed Brown #undef __FUNCT__ 2284*0e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 2285*0e4ef248SJed Brown /*@C 2286*0e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 2287*0e4ef248SJed Brown 2288*0e4ef248SJed Brown Collective on TS 2289*0e4ef248SJed Brown 2290*0e4ef248SJed Brown Input Arguments: 2291*0e4ef248SJed Brown + ts - time stepping context 2292*0e4ef248SJed Brown . t - time at which to evaluate 2293*0e4ef248SJed Brown . X - state at which to evaluate 2294*0e4ef248SJed Brown - ctx - context 2295*0e4ef248SJed Brown 2296*0e4ef248SJed Brown Output Arguments: 2297*0e4ef248SJed Brown . F - right hand side 2298*0e4ef248SJed Brown 2299*0e4ef248SJed Brown Level: intermediate 2300*0e4ef248SJed Brown 2301*0e4ef248SJed Brown Notes: 2302*0e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 2303*0e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 2304*0e4ef248SJed Brown 2305*0e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 2306*0e4ef248SJed Brown @*/ 2307*0e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 2308*0e4ef248SJed Brown { 2309*0e4ef248SJed Brown PetscErrorCode ierr; 2310*0e4ef248SJed Brown Mat Arhs,Brhs; 2311*0e4ef248SJed Brown MatStructure flg2; 2312*0e4ef248SJed Brown 2313*0e4ef248SJed Brown PetscFunctionBegin; 2314*0e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 2315*0e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 2316*0e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 2317*0e4ef248SJed Brown PetscFunctionReturn(0); 2318*0e4ef248SJed Brown } 2319*0e4ef248SJed Brown 2320*0e4ef248SJed Brown #undef __FUNCT__ 2321*0e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 2322*0e4ef248SJed Brown /*@C 2323*0e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 2324*0e4ef248SJed Brown 2325*0e4ef248SJed Brown Collective on TS 2326*0e4ef248SJed Brown 2327*0e4ef248SJed Brown Input Arguments: 2328*0e4ef248SJed Brown + ts - time stepping context 2329*0e4ef248SJed Brown . t - time at which to evaluate 2330*0e4ef248SJed Brown . X - state at which to evaluate 2331*0e4ef248SJed Brown - ctx - context 2332*0e4ef248SJed Brown 2333*0e4ef248SJed Brown Output Arguments: 2334*0e4ef248SJed Brown + A - pointer to operator 2335*0e4ef248SJed Brown . B - pointer to preconditioning matrix 2336*0e4ef248SJed Brown - flg - matrix structure flag 2337*0e4ef248SJed Brown 2338*0e4ef248SJed Brown Level: intermediate 2339*0e4ef248SJed Brown 2340*0e4ef248SJed Brown Notes: 2341*0e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 2342*0e4ef248SJed Brown 2343*0e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 2344*0e4ef248SJed Brown @*/ 2345*0e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 2346*0e4ef248SJed Brown { 2347*0e4ef248SJed Brown 2348*0e4ef248SJed Brown PetscFunctionBegin; 2349*0e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 2350*0e4ef248SJed Brown PetscFunctionReturn(0); 2351*0e4ef248SJed Brown } 2352*0e4ef248SJed Brown 2353325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2354c6db04a5SJed Brown #include <mex.h> 2355325fc9f4SBarry Smith 2356325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2357325fc9f4SBarry Smith 2358325fc9f4SBarry Smith #undef __FUNCT__ 2359325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2360325fc9f4SBarry Smith /* 2361325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2362325fc9f4SBarry Smith TSSetFunctionMatlab(). 2363325fc9f4SBarry Smith 2364325fc9f4SBarry Smith Collective on TS 2365325fc9f4SBarry Smith 2366325fc9f4SBarry Smith Input Parameters: 2367325fc9f4SBarry Smith + snes - the TS context 2368325fc9f4SBarry Smith - x - input vector 2369325fc9f4SBarry Smith 2370325fc9f4SBarry Smith Output Parameter: 2371325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2372325fc9f4SBarry Smith 2373325fc9f4SBarry Smith Notes: 2374325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2375325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2376325fc9f4SBarry Smith themselves. 2377325fc9f4SBarry Smith 2378325fc9f4SBarry Smith Level: developer 2379325fc9f4SBarry Smith 2380325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2381325fc9f4SBarry Smith 2382325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2383325fc9f4SBarry Smith */ 23847087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2385325fc9f4SBarry Smith { 2386325fc9f4SBarry Smith PetscErrorCode ierr; 2387325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2388325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2389325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2390325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2391325fc9f4SBarry Smith 2392325fc9f4SBarry Smith PetscFunctionBegin; 2393325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2394325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2395325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2396325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2397325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2398325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2399325fc9f4SBarry Smith 2400325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2401325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2402880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2403325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2404325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2405325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2406325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2407325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2408325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2409325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2410325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2411325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2412325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2413325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2414325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2415325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2416325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2417325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2418325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2419325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2420325fc9f4SBarry Smith PetscFunctionReturn(0); 2421325fc9f4SBarry Smith } 2422325fc9f4SBarry Smith 2423325fc9f4SBarry Smith 2424325fc9f4SBarry Smith #undef __FUNCT__ 2425325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2426325fc9f4SBarry Smith /* 2427325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2428325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2429e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2430325fc9f4SBarry Smith 2431325fc9f4SBarry Smith Logically Collective on TS 2432325fc9f4SBarry Smith 2433325fc9f4SBarry Smith Input Parameters: 2434325fc9f4SBarry Smith + ts - the TS context 2435325fc9f4SBarry Smith - func - function evaluation routine 2436325fc9f4SBarry Smith 2437325fc9f4SBarry Smith Calling sequence of func: 2438325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2439325fc9f4SBarry Smith 2440325fc9f4SBarry Smith Level: beginner 2441325fc9f4SBarry Smith 2442325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2443325fc9f4SBarry Smith 2444325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2445325fc9f4SBarry Smith */ 24467087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2447325fc9f4SBarry Smith { 2448325fc9f4SBarry Smith PetscErrorCode ierr; 2449325fc9f4SBarry Smith TSMatlabContext *sctx; 2450325fc9f4SBarry Smith 2451325fc9f4SBarry Smith PetscFunctionBegin; 2452325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2453325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2454325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2455325fc9f4SBarry Smith /* 2456325fc9f4SBarry Smith This should work, but it doesn't 2457325fc9f4SBarry Smith sctx->ctx = ctx; 2458325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2459325fc9f4SBarry Smith */ 2460325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2461325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2462325fc9f4SBarry Smith PetscFunctionReturn(0); 2463325fc9f4SBarry Smith } 2464325fc9f4SBarry Smith 2465325fc9f4SBarry Smith #undef __FUNCT__ 2466325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2467325fc9f4SBarry Smith /* 2468325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2469325fc9f4SBarry Smith TSSetJacobianMatlab(). 2470325fc9f4SBarry Smith 2471325fc9f4SBarry Smith Collective on TS 2472325fc9f4SBarry Smith 2473325fc9f4SBarry Smith Input Parameters: 2474325fc9f4SBarry Smith + snes - the TS context 2475325fc9f4SBarry Smith . x - input vector 2476325fc9f4SBarry Smith . A, B - the matrices 2477325fc9f4SBarry Smith - ctx - user context 2478325fc9f4SBarry Smith 2479325fc9f4SBarry Smith Output Parameter: 2480325fc9f4SBarry Smith . flag - structure of the matrix 2481325fc9f4SBarry Smith 2482325fc9f4SBarry Smith Level: developer 2483325fc9f4SBarry Smith 2484325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2485325fc9f4SBarry Smith 2486325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2487325fc9f4SBarry Smith @*/ 24887087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2489325fc9f4SBarry Smith { 2490325fc9f4SBarry Smith PetscErrorCode ierr; 2491325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2492325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2493325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2494325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2495325fc9f4SBarry Smith 2496325fc9f4SBarry Smith PetscFunctionBegin; 2497325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2498325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2499325fc9f4SBarry Smith 2500325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2501325fc9f4SBarry Smith 2502325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2503325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2504325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2505325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2506325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2507325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2508325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2509325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2510325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2511325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2512325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2513325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2514325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2515325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2516325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2517325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2518325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2519325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2520325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2521325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2522325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2523325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2524325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2525325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2526325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2527325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2528325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2529325fc9f4SBarry Smith PetscFunctionReturn(0); 2530325fc9f4SBarry Smith } 2531325fc9f4SBarry Smith 2532325fc9f4SBarry Smith 2533325fc9f4SBarry Smith #undef __FUNCT__ 2534325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2535325fc9f4SBarry Smith /* 2536325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2537e3c5b3baSBarry 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 2538325fc9f4SBarry Smith 2539325fc9f4SBarry Smith Logically Collective on TS 2540325fc9f4SBarry Smith 2541325fc9f4SBarry Smith Input Parameters: 2542325fc9f4SBarry Smith + snes - the TS context 2543325fc9f4SBarry Smith . A,B - Jacobian matrices 2544325fc9f4SBarry Smith . func - function evaluation routine 2545325fc9f4SBarry Smith - ctx - user context 2546325fc9f4SBarry Smith 2547325fc9f4SBarry Smith Calling sequence of func: 2548325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2549325fc9f4SBarry Smith 2550325fc9f4SBarry Smith 2551325fc9f4SBarry Smith Level: developer 2552325fc9f4SBarry Smith 2553325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2554325fc9f4SBarry Smith 2555325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2556325fc9f4SBarry Smith */ 25577087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2558325fc9f4SBarry Smith { 2559325fc9f4SBarry Smith PetscErrorCode ierr; 2560325fc9f4SBarry Smith TSMatlabContext *sctx; 2561325fc9f4SBarry Smith 2562325fc9f4SBarry Smith PetscFunctionBegin; 2563325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2564325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2565325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2566325fc9f4SBarry Smith /* 2567325fc9f4SBarry Smith This should work, but it doesn't 2568325fc9f4SBarry Smith sctx->ctx = ctx; 2569325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2570325fc9f4SBarry Smith */ 2571325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2572325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2573325fc9f4SBarry Smith PetscFunctionReturn(0); 2574325fc9f4SBarry Smith } 2575325fc9f4SBarry Smith 2576b5b1a830SBarry Smith #undef __FUNCT__ 2577b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2578b5b1a830SBarry Smith /* 2579b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2580b5b1a830SBarry Smith 2581b5b1a830SBarry Smith Collective on TS 2582b5b1a830SBarry Smith 2583b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2584b5b1a830SBarry Smith @*/ 25857087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2586b5b1a830SBarry Smith { 2587b5b1a830SBarry Smith PetscErrorCode ierr; 2588b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2589a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2590b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2591b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2592b5b1a830SBarry Smith 2593b5b1a830SBarry Smith PetscFunctionBegin; 2594b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2595b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2596b5b1a830SBarry Smith 2597b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2598b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2599b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2600b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2601b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2602b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2603b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2604b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2605b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2606b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2607b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2608b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2609b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2610b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2611b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2612b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2613b5b1a830SBarry Smith PetscFunctionReturn(0); 2614b5b1a830SBarry Smith } 2615b5b1a830SBarry Smith 2616b5b1a830SBarry Smith 2617b5b1a830SBarry Smith #undef __FUNCT__ 2618b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2619b5b1a830SBarry Smith /* 2620b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2621b5b1a830SBarry Smith 2622b5b1a830SBarry Smith Level: developer 2623b5b1a830SBarry Smith 2624b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2625b5b1a830SBarry Smith 2626b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2627b5b1a830SBarry Smith */ 26287087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2629b5b1a830SBarry Smith { 2630b5b1a830SBarry Smith PetscErrorCode ierr; 2631b5b1a830SBarry Smith TSMatlabContext *sctx; 2632b5b1a830SBarry Smith 2633b5b1a830SBarry Smith PetscFunctionBegin; 2634b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2635b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2636b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2637b5b1a830SBarry Smith /* 2638b5b1a830SBarry Smith This should work, but it doesn't 2639b5b1a830SBarry Smith sctx->ctx = ctx; 2640b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2641b5b1a830SBarry Smith */ 2642b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2643b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2644b5b1a830SBarry Smith PetscFunctionReturn(0); 2645b5b1a830SBarry Smith } 2646b5b1a830SBarry Smith 2647325fc9f4SBarry Smith #endif 2648