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; 2130e4ef248SJed 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); 2190e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 2200e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 2210e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2220e4ef248SJed Brown PetscFunctionReturn(0); 223d90be118SSean Farley 2243b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 225d90be118SSean Farley 2263b5f76d0SSean Farley if (ts->userops->rhsjacobian) { 2270e4ef248SJed Brown } 228d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 229a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 230a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 2313b5f76d0SSean Farley ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 232a7a1495cSBarry Smith PetscStackPop; 233d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 234a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2350700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2360700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 237ef66eb69SBarry Smith } else { 238214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 239214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 240214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 241ef66eb69SBarry Smith } 2420e4ef248SJed Brown ts->rhsjacobian.time = t; 2430e4ef248SJed Brown ts->rhsjacobian.X = X; 2440e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2450e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 246a7a1495cSBarry Smith PetscFunctionReturn(0); 247a7a1495cSBarry Smith } 248a7a1495cSBarry Smith 2494a2ae208SSatish Balay #undef __FUNCT__ 2504a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 251316643e7SJed Brown /*@ 252d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 253d763cef2SBarry Smith 254316643e7SJed Brown Collective on TS and Vec 255316643e7SJed Brown 256316643e7SJed Brown Input Parameters: 257316643e7SJed Brown + ts - the TS context 258316643e7SJed Brown . t - current time 259316643e7SJed Brown - x - state vector 260316643e7SJed Brown 261316643e7SJed Brown Output Parameter: 262316643e7SJed Brown . y - right hand side 263316643e7SJed Brown 264316643e7SJed Brown Note: 265316643e7SJed Brown Most users should not need to explicitly call this routine, as it 266316643e7SJed Brown is used internally within the nonlinear solvers. 267316643e7SJed Brown 268316643e7SJed Brown Level: developer 269316643e7SJed Brown 270316643e7SJed Brown .keywords: TS, compute 271316643e7SJed Brown 272316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 273316643e7SJed Brown @*/ 274dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 275d763cef2SBarry Smith { 276dfbe8321SBarry Smith PetscErrorCode ierr; 277d763cef2SBarry Smith 278d763cef2SBarry Smith PetscFunctionBegin; 2790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2800700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2810700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 282d763cef2SBarry Smith 2833b5f76d0SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 284d763cef2SBarry Smith 285d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 2863b5f76d0SSean Farley if (ts->userops->rhsfunction) { 287d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 2883b5f76d0SSean Farley ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 289d763cef2SBarry Smith PetscStackPop; 290214bc6a2SJed Brown } else { 291214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 292b2cd27e8SJed Brown } 29344a41b28SSean Farley 294d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 295d763cef2SBarry Smith PetscFunctionReturn(0); 296d763cef2SBarry Smith } 297d763cef2SBarry Smith 2984a2ae208SSatish Balay #undef __FUNCT__ 299214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 300214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 301214bc6a2SJed Brown { 302214bc6a2SJed Brown PetscErrorCode ierr; 303214bc6a2SJed Brown 304214bc6a2SJed Brown PetscFunctionBegin; 305214bc6a2SJed Brown if (!ts->Frhs) { 306214bc6a2SJed Brown ierr = VecDuplicate(ts->vec_sol,&ts->Frhs);CHKERRQ(ierr); 307214bc6a2SJed Brown } 308214bc6a2SJed Brown *Frhs = ts->Frhs; 309214bc6a2SJed Brown PetscFunctionReturn(0); 310214bc6a2SJed Brown } 311214bc6a2SJed Brown 312214bc6a2SJed Brown #undef __FUNCT__ 313214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 314214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 315214bc6a2SJed Brown { 316214bc6a2SJed Brown PetscErrorCode ierr; 317214bc6a2SJed Brown Mat A,B; 318214bc6a2SJed Brown 319214bc6a2SJed Brown PetscFunctionBegin; 320214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 321214bc6a2SJed Brown if (Arhs) { 322214bc6a2SJed Brown if (!ts->Arhs) { 323214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 324214bc6a2SJed Brown } 325214bc6a2SJed Brown *Arhs = ts->Arhs; 326214bc6a2SJed Brown } 327214bc6a2SJed Brown if (Brhs) { 328214bc6a2SJed Brown if (!ts->Brhs) { 329214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 330214bc6a2SJed Brown } 331214bc6a2SJed Brown *Brhs = ts->Brhs; 332214bc6a2SJed Brown } 333214bc6a2SJed Brown PetscFunctionReturn(0); 334214bc6a2SJed Brown } 335214bc6a2SJed Brown 336214bc6a2SJed Brown #undef __FUNCT__ 337316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 338316643e7SJed Brown /*@ 339316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 340316643e7SJed Brown 341316643e7SJed Brown Collective on TS and Vec 342316643e7SJed Brown 343316643e7SJed Brown Input Parameters: 344316643e7SJed Brown + ts - the TS context 345316643e7SJed Brown . t - current time 346316643e7SJed Brown . X - state vector 347214bc6a2SJed Brown . Xdot - time derivative of state vector 348214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 349316643e7SJed Brown 350316643e7SJed Brown Output Parameter: 351316643e7SJed Brown . Y - right hand side 352316643e7SJed Brown 353316643e7SJed Brown Note: 354316643e7SJed Brown Most users should not need to explicitly call this routine, as it 355316643e7SJed Brown is used internally within the nonlinear solvers. 356316643e7SJed Brown 357316643e7SJed Brown If the user did did not write their equations in implicit form, this 358316643e7SJed Brown function recasts them in implicit form. 359316643e7SJed Brown 360316643e7SJed Brown Level: developer 361316643e7SJed Brown 362316643e7SJed Brown .keywords: TS, compute 363316643e7SJed Brown 364316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 365316643e7SJed Brown @*/ 366214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 367316643e7SJed Brown { 368316643e7SJed Brown PetscErrorCode ierr; 369316643e7SJed Brown 370316643e7SJed Brown PetscFunctionBegin; 3710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3720700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3730700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3740700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 375316643e7SJed Brown 376d90be118SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 377d90be118SSean Farley 378316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 3793b5f76d0SSean Farley if (ts->userops->ifunction) { 380316643e7SJed Brown PetscStackPush("TS user implicit function"); 3813b5f76d0SSean Farley ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 382316643e7SJed Brown PetscStackPop; 383214bc6a2SJed Brown } 384214bc6a2SJed Brown if (imex) { 3853b5f76d0SSean Farley if (!ts->userops->ifunction) {ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);} 386316643e7SJed Brown } else { 3873b5f76d0SSean Farley if (!ts->userops->ifunction) { 388089b2837SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 389ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 390214bc6a2SJed Brown } else { 391214bc6a2SJed Brown Vec Frhs; 392214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 393214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 394214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 395316643e7SJed Brown } 3964a6899ffSJed Brown } 397316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 398316643e7SJed Brown PetscFunctionReturn(0); 399316643e7SJed Brown } 400316643e7SJed Brown 401316643e7SJed Brown #undef __FUNCT__ 402316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 403316643e7SJed Brown /*@ 404316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 405316643e7SJed Brown 406316643e7SJed Brown Collective on TS and Vec 407316643e7SJed Brown 408316643e7SJed Brown Input 409316643e7SJed Brown Input Parameters: 410316643e7SJed Brown + ts - the TS context 411316643e7SJed Brown . t - current timestep 412316643e7SJed Brown . X - state vector 413316643e7SJed Brown . Xdot - time derivative of state vector 414214bc6a2SJed Brown . shift - shift to apply, see note below 415214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 416316643e7SJed Brown 417316643e7SJed Brown Output Parameters: 418316643e7SJed Brown + A - Jacobian matrix 419316643e7SJed Brown . B - optional preconditioning matrix 420316643e7SJed Brown - flag - flag indicating matrix structure 421316643e7SJed Brown 422316643e7SJed Brown Notes: 423316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 424316643e7SJed Brown 425316643e7SJed Brown dF/dX + shift*dF/dXdot 426316643e7SJed Brown 427316643e7SJed Brown Most users should not need to explicitly call this routine, as it 428316643e7SJed Brown is used internally within the nonlinear solvers. 429316643e7SJed Brown 430316643e7SJed Brown Level: developer 431316643e7SJed Brown 432316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 433316643e7SJed Brown 434316643e7SJed Brown .seealso: TSSetIJacobian() 43563495f91SJed Brown @*/ 436214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 437316643e7SJed Brown { 438316643e7SJed Brown PetscErrorCode ierr; 439316643e7SJed Brown 440316643e7SJed Brown PetscFunctionBegin; 4410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4420700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4430700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 444316643e7SJed Brown PetscValidPointer(A,6); 4450700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 446316643e7SJed Brown PetscValidPointer(B,7); 4470700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 448316643e7SJed Brown PetscValidPointer(flg,8); 449316643e7SJed Brown 4503b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 451316643e7SJed Brown 4524e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 453316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 4543b5f76d0SSean Farley if (ts->userops->ijacobian) { 455316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 4563b5f76d0SSean Farley ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 457316643e7SJed Brown PetscStackPop; 458214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 459214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 460214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 4614a6899ffSJed Brown } 462214bc6a2SJed Brown if (imex) { 4633b5f76d0SSean Farley if (!ts->userops->ijacobian) { /* system was written as Xdot = F(t,X) */ 464214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 465214bc6a2SJed Brown ierr = MatShift(*A,1.0);CHKERRQ(ierr); 466214bc6a2SJed Brown if (*A != *B) { 467214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 468214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 469214bc6a2SJed Brown } 470214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 471214bc6a2SJed Brown } 472214bc6a2SJed Brown } else { 4733b5f76d0SSean Farley if (!ts->userops->ijacobian) { 474214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 475214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 476214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 477316643e7SJed Brown if (*A != *B) { 478316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 479316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 480316643e7SJed Brown } 4813b5f76d0SSean Farley } else if (ts->userops->rhsjacobian) { 482214bc6a2SJed Brown Mat Arhs,Brhs; 483214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 484214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 485214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 486214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 487214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 488214bc6a2SJed Brown if (*A != *B) { 489214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 490214bc6a2SJed Brown } 491214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 492214bc6a2SJed Brown } 493316643e7SJed Brown } 494316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 495316643e7SJed Brown PetscFunctionReturn(0); 496316643e7SJed Brown } 497316643e7SJed Brown 498316643e7SJed Brown #undef __FUNCT__ 4994a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 500d763cef2SBarry Smith /*@C 501d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 502d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 503d763cef2SBarry Smith 5043f9fe445SBarry Smith Logically Collective on TS 505d763cef2SBarry Smith 506d763cef2SBarry Smith Input Parameters: 507d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 508ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 509d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 510d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 511d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 512d763cef2SBarry Smith 513d763cef2SBarry Smith Calling sequence of func: 51487828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 515d763cef2SBarry Smith 516d763cef2SBarry Smith + t - current timestep 517d763cef2SBarry Smith . u - input vector 518d763cef2SBarry Smith . F - function vector 519d763cef2SBarry Smith - ctx - [optional] user-defined function context 520d763cef2SBarry Smith 521d763cef2SBarry Smith Important: 52276f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 523d763cef2SBarry Smith 524d763cef2SBarry Smith Level: beginner 525d763cef2SBarry Smith 526d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 527d763cef2SBarry Smith 52876f2fa84SHong Zhang .seealso: TSSetMatrices() 529d763cef2SBarry Smith @*/ 530089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 531d763cef2SBarry Smith { 532089b2837SJed Brown PetscErrorCode ierr; 533089b2837SJed Brown SNES snes; 534d763cef2SBarry Smith 535089b2837SJed Brown PetscFunctionBegin; 5360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 537ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 5383b5f76d0SSean Farley if (f) ts->userops->rhsfunction = f; 5394e684422SJed Brown if (ctx) ts->funP = ctx; 540089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 541089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 542d763cef2SBarry Smith PetscFunctionReturn(0); 543d763cef2SBarry Smith } 544d763cef2SBarry Smith 5454a2ae208SSatish Balay #undef __FUNCT__ 5464a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 547d763cef2SBarry Smith /*@C 548d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 549d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 55076f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 551d763cef2SBarry Smith 5523f9fe445SBarry Smith Logically Collective on TS 553d763cef2SBarry Smith 554d763cef2SBarry Smith Input Parameters: 555d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 556d763cef2SBarry Smith . A - Jacobian matrix 557d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 558d763cef2SBarry Smith . f - the Jacobian evaluation routine 559d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 560d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 561d763cef2SBarry Smith 562d763cef2SBarry Smith Calling sequence of func: 56387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 564d763cef2SBarry Smith 565d763cef2SBarry Smith + t - current timestep 566d763cef2SBarry Smith . u - input vector 567d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 568d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 569d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 57094b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 571d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 572d763cef2SBarry Smith 573d763cef2SBarry Smith Notes: 57494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 575d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 576d763cef2SBarry Smith 577d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 578d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 57956335db2SHong Zhang completely new matrix structure (not just different matrix elements) 580d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 581d763cef2SBarry Smith throughout the global iterations. 582d763cef2SBarry Smith 583d763cef2SBarry Smith Level: beginner 584d763cef2SBarry Smith 585d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 586d763cef2SBarry Smith 587d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 58876f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 589d763cef2SBarry Smith 590d763cef2SBarry Smith @*/ 591089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 592d763cef2SBarry Smith { 593277b19d0SLisandro Dalcin PetscErrorCode ierr; 594089b2837SJed Brown SNES snes; 595277b19d0SLisandro Dalcin 596d763cef2SBarry Smith PetscFunctionBegin; 5970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 598277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 599277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 600277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 601277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 602d763cef2SBarry Smith 6033b5f76d0SSean Farley if (f) ts->userops->rhsjacobian = f; 604277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 605089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 606*dabe61deSJed Brown if (!ts->userops->ijacobian) { 607089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 6080e4ef248SJed Brown } 6090e4ef248SJed Brown if (A) { 6100e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 6110e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 6120e4ef248SJed Brown ts->Arhs = A; 6130e4ef248SJed Brown } 6140e4ef248SJed Brown if (B) { 6150e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 6160e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 6170e4ef248SJed Brown ts->Brhs = B; 6180e4ef248SJed Brown } 619d763cef2SBarry Smith PetscFunctionReturn(0); 620d763cef2SBarry Smith } 621d763cef2SBarry Smith 622316643e7SJed Brown 623316643e7SJed Brown #undef __FUNCT__ 624316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 625316643e7SJed Brown /*@C 626316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 627316643e7SJed Brown 6283f9fe445SBarry Smith Logically Collective on TS 629316643e7SJed Brown 630316643e7SJed Brown Input Parameters: 631316643e7SJed Brown + ts - the TS context obtained from TSCreate() 632ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 633316643e7SJed Brown . f - the function evaluation routine 634316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 635316643e7SJed Brown 636316643e7SJed Brown Calling sequence of f: 637316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 638316643e7SJed Brown 639316643e7SJed Brown + t - time at step/stage being solved 640316643e7SJed Brown . u - state vector 641316643e7SJed Brown . u_t - time derivative of state vector 642316643e7SJed Brown . F - function vector 643316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 644316643e7SJed Brown 645316643e7SJed Brown Important: 646316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 647316643e7SJed Brown 648316643e7SJed Brown Level: beginner 649316643e7SJed Brown 650316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 651316643e7SJed Brown 652316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 653316643e7SJed Brown @*/ 654089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 655316643e7SJed Brown { 656089b2837SJed Brown PetscErrorCode ierr; 657089b2837SJed Brown SNES snes; 658316643e7SJed Brown 659316643e7SJed Brown PetscFunctionBegin; 6600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 661ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 6623b5f76d0SSean Farley if (f) ts->userops->ifunction = f; 663089b2837SJed Brown if (ctx) ts->funP = ctx; 664089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 665089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 666089b2837SJed Brown PetscFunctionReturn(0); 667089b2837SJed Brown } 668089b2837SJed Brown 669089b2837SJed Brown #undef __FUNCT__ 670089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 671089b2837SJed Brown /*@C 672089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 673089b2837SJed Brown 674089b2837SJed Brown Not Collective 675089b2837SJed Brown 676089b2837SJed Brown Input Parameter: 677089b2837SJed Brown . ts - the TS context 678089b2837SJed Brown 679089b2837SJed Brown Output Parameter: 680089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 681089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 682089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 683089b2837SJed Brown 684089b2837SJed Brown Level: advanced 685089b2837SJed Brown 686089b2837SJed Brown .keywords: TS, nonlinear, get, function 687089b2837SJed Brown 688089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 689089b2837SJed Brown @*/ 690089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 691089b2837SJed Brown { 692089b2837SJed Brown PetscErrorCode ierr; 693089b2837SJed Brown SNES snes; 694089b2837SJed Brown 695089b2837SJed Brown PetscFunctionBegin; 696089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 697089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 698089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 6993b5f76d0SSean Farley if (func) *func = ts->userops->ifunction; 700089b2837SJed Brown if (ctx) *ctx = ts->funP; 701089b2837SJed Brown PetscFunctionReturn(0); 702089b2837SJed Brown } 703089b2837SJed Brown 704089b2837SJed Brown #undef __FUNCT__ 705089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 706089b2837SJed Brown /*@C 707089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 708089b2837SJed Brown 709089b2837SJed Brown Not Collective 710089b2837SJed Brown 711089b2837SJed Brown Input Parameter: 712089b2837SJed Brown . ts - the TS context 713089b2837SJed Brown 714089b2837SJed Brown Output Parameter: 715089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 716089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 717089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 718089b2837SJed Brown 719089b2837SJed Brown Level: advanced 720089b2837SJed Brown 721089b2837SJed Brown .keywords: TS, nonlinear, get, function 722089b2837SJed Brown 723089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 724089b2837SJed Brown @*/ 725089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 726089b2837SJed Brown { 727089b2837SJed Brown PetscErrorCode ierr; 728089b2837SJed Brown SNES snes; 729089b2837SJed Brown 730089b2837SJed Brown PetscFunctionBegin; 731089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 732089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 733089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7343b5f76d0SSean Farley if (func) *func = ts->userops->rhsfunction; 735089b2837SJed Brown if (ctx) *ctx = ts->funP; 736316643e7SJed Brown PetscFunctionReturn(0); 737316643e7SJed Brown } 738316643e7SJed Brown 739316643e7SJed Brown #undef __FUNCT__ 740316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 741316643e7SJed Brown /*@C 742a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 743a4f0a591SBarry Smith you provided with TSSetIFunction(). 744316643e7SJed Brown 7453f9fe445SBarry Smith Logically Collective on TS 746316643e7SJed Brown 747316643e7SJed Brown Input Parameters: 748316643e7SJed Brown + ts - the TS context obtained from TSCreate() 749316643e7SJed Brown . A - Jacobian matrix 750316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 751316643e7SJed Brown . f - the Jacobian evaluation routine 752316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 753316643e7SJed Brown 754316643e7SJed Brown Calling sequence of f: 7551b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 756316643e7SJed Brown 757316643e7SJed Brown + t - time at step/stage being solved 7581b4a444bSJed Brown . U - state vector 7591b4a444bSJed Brown . U_t - time derivative of state vector 760316643e7SJed Brown . a - shift 7611b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 762316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 763316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 764316643e7SJed Brown structure (same as flag in KSPSetOperators()) 765316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 766316643e7SJed Brown 767316643e7SJed Brown Notes: 768316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 769316643e7SJed Brown 770a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 771a4f0a591SBarry 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. 772a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 773a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 774a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 775a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 776a4f0a591SBarry Smith 777316643e7SJed Brown Level: beginner 778316643e7SJed Brown 779316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 780316643e7SJed Brown 781316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 782316643e7SJed Brown 783316643e7SJed Brown @*/ 7847087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 785316643e7SJed Brown { 786316643e7SJed Brown PetscErrorCode ierr; 787089b2837SJed Brown SNES snes; 788316643e7SJed Brown 789316643e7SJed Brown PetscFunctionBegin; 7900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7910700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 7920700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 793316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 794316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 7953b5f76d0SSean Farley if (f) ts->userops->ijacobian = f; 796316643e7SJed Brown if (ctx) ts->jacP = ctx; 797089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 798089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 799316643e7SJed Brown PetscFunctionReturn(0); 800316643e7SJed Brown } 801316643e7SJed Brown 8024a2ae208SSatish Balay #undef __FUNCT__ 8034a2ae208SSatish Balay #define __FUNCT__ "TSView" 8047e2c5f70SBarry Smith /*@C 805d763cef2SBarry Smith TSView - Prints the TS data structure. 806d763cef2SBarry Smith 8074c49b128SBarry Smith Collective on TS 808d763cef2SBarry Smith 809d763cef2SBarry Smith Input Parameters: 810d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 811d763cef2SBarry Smith - viewer - visualization context 812d763cef2SBarry Smith 813d763cef2SBarry Smith Options Database Key: 814d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 815d763cef2SBarry Smith 816d763cef2SBarry Smith Notes: 817d763cef2SBarry Smith The available visualization contexts include 818b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 819b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 820d763cef2SBarry Smith output where only the first processor opens 821d763cef2SBarry Smith the file. All other processors send their 822d763cef2SBarry Smith data to the first processor to print. 823d763cef2SBarry Smith 824d763cef2SBarry Smith The user can open an alternative visualization context with 825b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 826d763cef2SBarry Smith 827d763cef2SBarry Smith Level: beginner 828d763cef2SBarry Smith 829d763cef2SBarry Smith .keywords: TS, timestep, view 830d763cef2SBarry Smith 831b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 832d763cef2SBarry Smith @*/ 8337087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 834d763cef2SBarry Smith { 835dfbe8321SBarry Smith PetscErrorCode ierr; 836a313700dSBarry Smith const TSType type; 837089b2837SJed Brown PetscBool iascii,isstring,isundials; 838d763cef2SBarry Smith 839d763cef2SBarry Smith PetscFunctionBegin; 8400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8413050cee2SBarry Smith if (!viewer) { 8427adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8433050cee2SBarry Smith } 8440700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 845c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 846fd16b177SBarry Smith 8472692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8482692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 84932077d6dSBarry Smith if (iascii) { 850317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 851000e7ae3SMatthew Knepley if (ts->ops->view) { 852b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 853000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 854b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 855d763cef2SBarry Smith } 85677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 857a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 858d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 85977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 860193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr); 861d763cef2SBarry Smith } 86277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 863193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 8640f5bd95cSBarry Smith } else if (isstring) { 865a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 866b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 867d763cef2SBarry Smith } 868b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 869089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 870089b2837SJed Brown if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 871b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 872d763cef2SBarry Smith PetscFunctionReturn(0); 873d763cef2SBarry Smith } 874d763cef2SBarry Smith 875d763cef2SBarry Smith 8764a2ae208SSatish Balay #undef __FUNCT__ 8774a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 878b07ff414SBarry Smith /*@ 879d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 880d763cef2SBarry Smith the timesteppers. 881d763cef2SBarry Smith 8823f9fe445SBarry Smith Logically Collective on TS 883d763cef2SBarry Smith 884d763cef2SBarry Smith Input Parameters: 885d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 886d763cef2SBarry Smith - usrP - optional user context 887d763cef2SBarry Smith 888d763cef2SBarry Smith Level: intermediate 889d763cef2SBarry Smith 890d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 891d763cef2SBarry Smith 892d763cef2SBarry Smith .seealso: TSGetApplicationContext() 893d763cef2SBarry Smith @*/ 8947087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 895d763cef2SBarry Smith { 896d763cef2SBarry Smith PetscFunctionBegin; 8970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 898d763cef2SBarry Smith ts->user = usrP; 899d763cef2SBarry Smith PetscFunctionReturn(0); 900d763cef2SBarry Smith } 901d763cef2SBarry Smith 9024a2ae208SSatish Balay #undef __FUNCT__ 9034a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 904b07ff414SBarry Smith /*@ 905d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 906d763cef2SBarry Smith timestepper. 907d763cef2SBarry Smith 908d763cef2SBarry Smith Not Collective 909d763cef2SBarry Smith 910d763cef2SBarry Smith Input Parameter: 911d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 912d763cef2SBarry Smith 913d763cef2SBarry Smith Output Parameter: 914d763cef2SBarry Smith . usrP - user context 915d763cef2SBarry Smith 916d763cef2SBarry Smith Level: intermediate 917d763cef2SBarry Smith 918d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 919d763cef2SBarry Smith 920d763cef2SBarry Smith .seealso: TSSetApplicationContext() 921d763cef2SBarry Smith @*/ 922e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 923d763cef2SBarry Smith { 924d763cef2SBarry Smith PetscFunctionBegin; 9250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 926e71120c6SJed Brown *(void**)usrP = ts->user; 927d763cef2SBarry Smith PetscFunctionReturn(0); 928d763cef2SBarry Smith } 929d763cef2SBarry Smith 9304a2ae208SSatish Balay #undef __FUNCT__ 9314a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 932d763cef2SBarry Smith /*@ 933d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 934d763cef2SBarry Smith 935d763cef2SBarry Smith Not Collective 936d763cef2SBarry Smith 937d763cef2SBarry Smith Input Parameter: 938d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 939d763cef2SBarry Smith 940d763cef2SBarry Smith Output Parameter: 941d763cef2SBarry Smith . iter - number steps so far 942d763cef2SBarry Smith 943d763cef2SBarry Smith Level: intermediate 944d763cef2SBarry Smith 945d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 946d763cef2SBarry Smith @*/ 9477087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 948d763cef2SBarry Smith { 949d763cef2SBarry Smith PetscFunctionBegin; 9500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9514482741eSBarry Smith PetscValidIntPointer(iter,2); 952d763cef2SBarry Smith *iter = ts->steps; 953d763cef2SBarry Smith PetscFunctionReturn(0); 954d763cef2SBarry Smith } 955d763cef2SBarry Smith 9564a2ae208SSatish Balay #undef __FUNCT__ 9574a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 958d763cef2SBarry Smith /*@ 959d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 960d763cef2SBarry Smith as well as the initial time. 961d763cef2SBarry Smith 9623f9fe445SBarry Smith Logically Collective on TS 963d763cef2SBarry Smith 964d763cef2SBarry Smith Input Parameters: 965d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 966d763cef2SBarry Smith . initial_time - the initial time 967d763cef2SBarry Smith - time_step - the size of the timestep 968d763cef2SBarry Smith 969d763cef2SBarry Smith Level: intermediate 970d763cef2SBarry Smith 971d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 972d763cef2SBarry Smith 973d763cef2SBarry Smith .keywords: TS, set, initial, timestep 974d763cef2SBarry Smith @*/ 9757087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 976d763cef2SBarry Smith { 977e144a568SJed Brown PetscErrorCode ierr; 978e144a568SJed Brown 979d763cef2SBarry Smith PetscFunctionBegin; 9800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 981e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 982d763cef2SBarry Smith ts->initial_time_step = time_step; 983d763cef2SBarry Smith ts->ptime = initial_time; 984d763cef2SBarry Smith PetscFunctionReturn(0); 985d763cef2SBarry Smith } 986d763cef2SBarry Smith 9874a2ae208SSatish Balay #undef __FUNCT__ 9884a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 989d763cef2SBarry Smith /*@ 990d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 991d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 992d763cef2SBarry Smith 9933f9fe445SBarry Smith Logically Collective on TS 994d763cef2SBarry Smith 995d763cef2SBarry Smith Input Parameters: 996d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 997d763cef2SBarry Smith - time_step - the size of the timestep 998d763cef2SBarry Smith 999d763cef2SBarry Smith Level: intermediate 1000d763cef2SBarry Smith 1001d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1002d763cef2SBarry Smith 1003d763cef2SBarry Smith .keywords: TS, set, timestep 1004d763cef2SBarry Smith @*/ 10057087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1006d763cef2SBarry Smith { 1007d763cef2SBarry Smith PetscFunctionBegin; 10080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1009c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1010d763cef2SBarry Smith ts->time_step = time_step; 1011193ac0bcSJed Brown ts->next_time_step = time_step; 1012d763cef2SBarry Smith PetscFunctionReturn(0); 1013d763cef2SBarry Smith } 1014d763cef2SBarry Smith 10154a2ae208SSatish Balay #undef __FUNCT__ 10164a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1017d763cef2SBarry Smith /*@ 1018d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1019d763cef2SBarry Smith 1020d763cef2SBarry Smith Not Collective 1021d763cef2SBarry Smith 1022d763cef2SBarry Smith Input Parameter: 1023d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1024d763cef2SBarry Smith 1025d763cef2SBarry Smith Output Parameter: 1026d763cef2SBarry Smith . dt - the current timestep size 1027d763cef2SBarry Smith 1028d763cef2SBarry Smith Level: intermediate 1029d763cef2SBarry Smith 1030d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1031d763cef2SBarry Smith 1032d763cef2SBarry Smith .keywords: TS, get, timestep 1033d763cef2SBarry Smith @*/ 10347087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1035d763cef2SBarry Smith { 1036d763cef2SBarry Smith PetscFunctionBegin; 10370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10384482741eSBarry Smith PetscValidDoublePointer(dt,2); 1039d763cef2SBarry Smith *dt = ts->time_step; 1040d763cef2SBarry Smith PetscFunctionReturn(0); 1041d763cef2SBarry Smith } 1042d763cef2SBarry Smith 10434a2ae208SSatish Balay #undef __FUNCT__ 10444a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1045d8e5e3e6SSatish Balay /*@ 1046d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1047d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1048d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1049d763cef2SBarry Smith the solution at the next timestep has been calculated. 1050d763cef2SBarry Smith 1051d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1052d763cef2SBarry Smith 1053d763cef2SBarry Smith Input Parameter: 1054d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1055d763cef2SBarry Smith 1056d763cef2SBarry Smith Output Parameter: 1057d763cef2SBarry Smith . v - the vector containing the solution 1058d763cef2SBarry Smith 1059d763cef2SBarry Smith Level: intermediate 1060d763cef2SBarry Smith 1061d763cef2SBarry Smith .seealso: TSGetTimeStep() 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1064d763cef2SBarry Smith @*/ 10657087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1066d763cef2SBarry Smith { 1067d763cef2SBarry Smith PetscFunctionBegin; 10680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10694482741eSBarry Smith PetscValidPointer(v,2); 10708737fe31SLisandro Dalcin *v = ts->vec_sol; 1071d763cef2SBarry Smith PetscFunctionReturn(0); 1072d763cef2SBarry Smith } 1073d763cef2SBarry Smith 1074bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 10754a2ae208SSatish Balay #undef __FUNCT__ 1076bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1077d8e5e3e6SSatish Balay /*@ 1078bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1079d763cef2SBarry Smith 1080bdad233fSMatthew Knepley Not collective 1081d763cef2SBarry Smith 1082bdad233fSMatthew Knepley Input Parameters: 1083bdad233fSMatthew Knepley + ts - The TS 1084bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1085d763cef2SBarry Smith .vb 1086d763cef2SBarry Smith U_t = A U 1087d763cef2SBarry Smith U_t = A(t) U 1088d763cef2SBarry Smith U_t = F(t,U) 1089d763cef2SBarry Smith .ve 1090d763cef2SBarry Smith 1091d763cef2SBarry Smith Level: beginner 1092d763cef2SBarry Smith 1093bdad233fSMatthew Knepley .keywords: TS, problem type 1094bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1095d763cef2SBarry Smith @*/ 10967087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1097a7cc72afSBarry Smith { 10989e2a6581SJed Brown PetscErrorCode ierr; 10999e2a6581SJed Brown 1100d763cef2SBarry Smith PetscFunctionBegin; 11010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1102bdad233fSMatthew Knepley ts->problem_type = type; 11039e2a6581SJed Brown if (type == TS_LINEAR) { 11049e2a6581SJed Brown SNES snes; 11059e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11069e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 11079e2a6581SJed Brown } 1108d763cef2SBarry Smith PetscFunctionReturn(0); 1109d763cef2SBarry Smith } 1110d763cef2SBarry Smith 1111bdad233fSMatthew Knepley #undef __FUNCT__ 1112bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1113bdad233fSMatthew Knepley /*@C 1114bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1115bdad233fSMatthew Knepley 1116bdad233fSMatthew Knepley Not collective 1117bdad233fSMatthew Knepley 1118bdad233fSMatthew Knepley Input Parameter: 1119bdad233fSMatthew Knepley . ts - The TS 1120bdad233fSMatthew Knepley 1121bdad233fSMatthew Knepley Output Parameter: 1122bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1123bdad233fSMatthew Knepley .vb 1124089b2837SJed Brown M U_t = A U 1125089b2837SJed Brown M(t) U_t = A(t) U 1126bdad233fSMatthew Knepley U_t = F(t,U) 1127bdad233fSMatthew Knepley .ve 1128bdad233fSMatthew Knepley 1129bdad233fSMatthew Knepley Level: beginner 1130bdad233fSMatthew Knepley 1131bdad233fSMatthew Knepley .keywords: TS, problem type 1132bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1133bdad233fSMatthew Knepley @*/ 11347087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1135a7cc72afSBarry Smith { 1136bdad233fSMatthew Knepley PetscFunctionBegin; 11370700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11384482741eSBarry Smith PetscValidIntPointer(type,2); 1139bdad233fSMatthew Knepley *type = ts->problem_type; 1140bdad233fSMatthew Knepley PetscFunctionReturn(0); 1141bdad233fSMatthew Knepley } 1142d763cef2SBarry Smith 11434a2ae208SSatish Balay #undef __FUNCT__ 11444a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1145d763cef2SBarry Smith /*@ 1146d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1147d763cef2SBarry Smith of a timestepper. 1148d763cef2SBarry Smith 1149d763cef2SBarry Smith Collective on TS 1150d763cef2SBarry Smith 1151d763cef2SBarry Smith Input Parameter: 1152d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1153d763cef2SBarry Smith 1154d763cef2SBarry Smith Notes: 1155d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1156d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1157d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1158d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1159d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1160d763cef2SBarry Smith 1161d763cef2SBarry Smith Level: advanced 1162d763cef2SBarry Smith 1163d763cef2SBarry Smith .keywords: TS, timestep, setup 1164d763cef2SBarry Smith 1165d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1166d763cef2SBarry Smith @*/ 11677087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1168d763cef2SBarry Smith { 1169dfbe8321SBarry Smith PetscErrorCode ierr; 1170d763cef2SBarry Smith 1171d763cef2SBarry Smith PetscFunctionBegin; 11720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1173277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1174277b19d0SLisandro Dalcin 11757adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 11769596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1177d763cef2SBarry Smith } 1178277b19d0SLisandro Dalcin 1179277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1180277b19d0SLisandro Dalcin 1181277b19d0SLisandro Dalcin if (ts->ops->setup) { 1182000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1183277b19d0SLisandro Dalcin } 1184277b19d0SLisandro Dalcin 1185277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1186277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1187277b19d0SLisandro Dalcin } 1188277b19d0SLisandro Dalcin 1189277b19d0SLisandro Dalcin #undef __FUNCT__ 1190277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1191277b19d0SLisandro Dalcin /*@ 1192277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1193277b19d0SLisandro Dalcin 1194277b19d0SLisandro Dalcin Collective on TS 1195277b19d0SLisandro Dalcin 1196277b19d0SLisandro Dalcin Input Parameter: 1197277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1198277b19d0SLisandro Dalcin 1199277b19d0SLisandro Dalcin Level: beginner 1200277b19d0SLisandro Dalcin 1201277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1202277b19d0SLisandro Dalcin 1203277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1204277b19d0SLisandro Dalcin @*/ 1205277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1206277b19d0SLisandro Dalcin { 1207277b19d0SLisandro Dalcin PetscErrorCode ierr; 1208277b19d0SLisandro Dalcin 1209277b19d0SLisandro Dalcin PetscFunctionBegin; 1210277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1211277b19d0SLisandro Dalcin if (ts->ops->reset) { 1212277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1213277b19d0SLisandro Dalcin } 1214277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12154e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 12164e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1217214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 12186bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1219277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1220277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1221d763cef2SBarry Smith PetscFunctionReturn(0); 1222d763cef2SBarry Smith } 1223d763cef2SBarry Smith 12244a2ae208SSatish Balay #undef __FUNCT__ 12254a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1226d8e5e3e6SSatish Balay /*@ 1227d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1228d763cef2SBarry Smith with TSCreate(). 1229d763cef2SBarry Smith 1230d763cef2SBarry Smith Collective on TS 1231d763cef2SBarry Smith 1232d763cef2SBarry Smith Input Parameter: 1233d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1234d763cef2SBarry Smith 1235d763cef2SBarry Smith Level: beginner 1236d763cef2SBarry Smith 1237d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1238d763cef2SBarry Smith 1239d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1240d763cef2SBarry Smith @*/ 12416bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1242d763cef2SBarry Smith { 12436849ba73SBarry Smith PetscErrorCode ierr; 1244d763cef2SBarry Smith 1245d763cef2SBarry Smith PetscFunctionBegin; 12466bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12476bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12486bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1249d763cef2SBarry Smith 12506bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1251277b19d0SLisandro Dalcin 1252be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12536bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 12546bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 12556d4c513bSLisandro Dalcin 12566bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 12576bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 12586bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 12596d4c513bSLisandro Dalcin 12603b5f76d0SSean Farley ierr = PetscFree((*ts)->userops); 12613b5f76d0SSean Farley 1262a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1263d763cef2SBarry Smith PetscFunctionReturn(0); 1264d763cef2SBarry Smith } 1265d763cef2SBarry Smith 12664a2ae208SSatish Balay #undef __FUNCT__ 12674a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1268d8e5e3e6SSatish Balay /*@ 1269d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1270d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1271d763cef2SBarry Smith 1272d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1273d763cef2SBarry Smith 1274d763cef2SBarry Smith Input Parameter: 1275d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1276d763cef2SBarry Smith 1277d763cef2SBarry Smith Output Parameter: 1278d763cef2SBarry Smith . snes - the nonlinear solver context 1279d763cef2SBarry Smith 1280d763cef2SBarry Smith Notes: 1281d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1282d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 128394b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1284d763cef2SBarry Smith 1285d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1286d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1287d763cef2SBarry Smith 1288d763cef2SBarry Smith Level: beginner 1289d763cef2SBarry Smith 1290d763cef2SBarry Smith .keywords: timestep, get, SNES 1291d763cef2SBarry Smith @*/ 12927087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1293d763cef2SBarry Smith { 1294d372ba47SLisandro Dalcin PetscErrorCode ierr; 1295d372ba47SLisandro Dalcin 1296d763cef2SBarry Smith PetscFunctionBegin; 12970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12984482741eSBarry Smith PetscValidPointer(snes,2); 1299d372ba47SLisandro Dalcin if (!ts->snes) { 1300d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1301d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1302d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 13039e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 13049e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 13059e2a6581SJed Brown } 1306d372ba47SLisandro Dalcin } 1307d763cef2SBarry Smith *snes = ts->snes; 1308d763cef2SBarry Smith PetscFunctionReturn(0); 1309d763cef2SBarry Smith } 1310d763cef2SBarry Smith 13114a2ae208SSatish Balay #undef __FUNCT__ 131294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1313d8e5e3e6SSatish Balay /*@ 131494b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1315d763cef2SBarry Smith a TS (timestepper) context. 1316d763cef2SBarry Smith 131794b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1318d763cef2SBarry Smith 1319d763cef2SBarry Smith Input Parameter: 1320d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1321d763cef2SBarry Smith 1322d763cef2SBarry Smith Output Parameter: 132394b7f48cSBarry Smith . ksp - the nonlinear solver context 1324d763cef2SBarry Smith 1325d763cef2SBarry Smith Notes: 132694b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1327d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1328d763cef2SBarry Smith KSP and PC contexts as well. 1329d763cef2SBarry Smith 133094b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 133194b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1332d763cef2SBarry Smith 1333d763cef2SBarry Smith Level: beginner 1334d763cef2SBarry Smith 133594b7f48cSBarry Smith .keywords: timestep, get, KSP 1336d763cef2SBarry Smith @*/ 13377087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1338d763cef2SBarry Smith { 1339d372ba47SLisandro Dalcin PetscErrorCode ierr; 1340089b2837SJed Brown SNES snes; 1341d372ba47SLisandro Dalcin 1342d763cef2SBarry Smith PetscFunctionBegin; 13430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13444482741eSBarry Smith PetscValidPointer(ksp,2); 134517186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1346e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1347089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1348089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1349d763cef2SBarry Smith PetscFunctionReturn(0); 1350d763cef2SBarry Smith } 1351d763cef2SBarry Smith 1352d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1353d763cef2SBarry Smith 13544a2ae208SSatish Balay #undef __FUNCT__ 1355adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1356adb62b0dSMatthew Knepley /*@ 1357adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1358adb62b0dSMatthew Knepley maximum time for iteration. 1359adb62b0dSMatthew Knepley 13603f9fe445SBarry Smith Not Collective 1361adb62b0dSMatthew Knepley 1362adb62b0dSMatthew Knepley Input Parameters: 1363adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1364adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1365adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1366adb62b0dSMatthew Knepley 1367adb62b0dSMatthew Knepley Level: intermediate 1368adb62b0dSMatthew Knepley 1369adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1370adb62b0dSMatthew Knepley @*/ 13717087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1372adb62b0dSMatthew Knepley { 1373adb62b0dSMatthew Knepley PetscFunctionBegin; 13740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1375abc0a331SBarry Smith if (maxsteps) { 13764482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1377adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1378adb62b0dSMatthew Knepley } 1379abc0a331SBarry Smith if (maxtime ) { 13804482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1381adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1382adb62b0dSMatthew Knepley } 1383adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1384adb62b0dSMatthew Knepley } 1385adb62b0dSMatthew Knepley 1386adb62b0dSMatthew Knepley #undef __FUNCT__ 13874a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1388d763cef2SBarry Smith /*@ 1389d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1390d763cef2SBarry Smith maximum time for iteration. 1391d763cef2SBarry Smith 13923f9fe445SBarry Smith Logically Collective on TS 1393d763cef2SBarry Smith 1394d763cef2SBarry Smith Input Parameters: 1395d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1396d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1397d763cef2SBarry Smith - maxtime - final time to iterate to 1398d763cef2SBarry Smith 1399d763cef2SBarry Smith Options Database Keys: 1400d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1401d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1402d763cef2SBarry Smith 1403d763cef2SBarry Smith Notes: 1404d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1405d763cef2SBarry Smith 1406d763cef2SBarry Smith Level: intermediate 1407d763cef2SBarry Smith 1408d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1409d763cef2SBarry Smith @*/ 14107087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1411d763cef2SBarry Smith { 1412d763cef2SBarry Smith PetscFunctionBegin; 14130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1414c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1415c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1416d763cef2SBarry Smith ts->max_steps = maxsteps; 1417d763cef2SBarry Smith ts->max_time = maxtime; 1418d763cef2SBarry Smith PetscFunctionReturn(0); 1419d763cef2SBarry Smith } 1420d763cef2SBarry Smith 14214a2ae208SSatish Balay #undef __FUNCT__ 14224a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1423d763cef2SBarry Smith /*@ 1424d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1425d763cef2SBarry Smith for use by the TS routines. 1426d763cef2SBarry Smith 14273f9fe445SBarry Smith Logically Collective on TS and Vec 1428d763cef2SBarry Smith 1429d763cef2SBarry Smith Input Parameters: 1430d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1431d763cef2SBarry Smith - x - the solution vector 1432d763cef2SBarry Smith 1433d763cef2SBarry Smith Level: beginner 1434d763cef2SBarry Smith 1435d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1436d763cef2SBarry Smith @*/ 14377087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1438d763cef2SBarry Smith { 14398737fe31SLisandro Dalcin PetscErrorCode ierr; 14408737fe31SLisandro Dalcin 1441d763cef2SBarry Smith PetscFunctionBegin; 14420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14430700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14448737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14456bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14468737fe31SLisandro Dalcin ts->vec_sol = x; 1447d763cef2SBarry Smith PetscFunctionReturn(0); 1448d763cef2SBarry Smith } 1449d763cef2SBarry Smith 1450e74ef692SMatthew Knepley #undef __FUNCT__ 1451e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1452ac226902SBarry Smith /*@C 1453000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14543f2090d5SJed Brown called once at the beginning of each time step. 1455000e7ae3SMatthew Knepley 14563f9fe445SBarry Smith Logically Collective on TS 1457000e7ae3SMatthew Knepley 1458000e7ae3SMatthew Knepley Input Parameters: 1459000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1460000e7ae3SMatthew Knepley - func - The function 1461000e7ae3SMatthew Knepley 1462000e7ae3SMatthew Knepley Calling sequence of func: 1463000e7ae3SMatthew Knepley . func (TS ts); 1464000e7ae3SMatthew Knepley 1465000e7ae3SMatthew Knepley Level: intermediate 1466000e7ae3SMatthew Knepley 1467000e7ae3SMatthew Knepley .keywords: TS, timestep 1468000e7ae3SMatthew Knepley @*/ 14697087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1470000e7ae3SMatthew Knepley { 1471000e7ae3SMatthew Knepley PetscFunctionBegin; 14720700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1473000e7ae3SMatthew Knepley ts->ops->prestep = func; 1474000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1475000e7ae3SMatthew Knepley } 1476000e7ae3SMatthew Knepley 1477e74ef692SMatthew Knepley #undef __FUNCT__ 14783f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 14793f2090d5SJed Brown /*@C 14803f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 14813f2090d5SJed Brown 14823f2090d5SJed Brown Collective on TS 14833f2090d5SJed Brown 14843f2090d5SJed Brown Input Parameters: 14853f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 14863f2090d5SJed Brown 14873f2090d5SJed Brown Notes: 14883f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 14893f2090d5SJed Brown so most users would not generally call this routine themselves. 14903f2090d5SJed Brown 14913f2090d5SJed Brown Level: developer 14923f2090d5SJed Brown 14933f2090d5SJed Brown .keywords: TS, timestep 14943f2090d5SJed Brown @*/ 14957087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 14963f2090d5SJed Brown { 14973f2090d5SJed Brown PetscErrorCode ierr; 14983f2090d5SJed Brown 14993f2090d5SJed Brown PetscFunctionBegin; 15000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 150172ac3e02SJed Brown if (ts->ops->prestep) { 15023f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15033f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15043f2090d5SJed Brown PetscStackPop; 1505312ce896SJed Brown } 15063f2090d5SJed Brown PetscFunctionReturn(0); 15073f2090d5SJed Brown } 15083f2090d5SJed Brown 15093f2090d5SJed Brown #undef __FUNCT__ 1510e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1511ac226902SBarry Smith /*@C 1512000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15133f2090d5SJed Brown called once at the end of each time step. 1514000e7ae3SMatthew Knepley 15153f9fe445SBarry Smith Logically Collective on TS 1516000e7ae3SMatthew Knepley 1517000e7ae3SMatthew Knepley Input Parameters: 1518000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1519000e7ae3SMatthew Knepley - func - The function 1520000e7ae3SMatthew Knepley 1521000e7ae3SMatthew Knepley Calling sequence of func: 1522000e7ae3SMatthew Knepley . func (TS ts); 1523000e7ae3SMatthew Knepley 1524000e7ae3SMatthew Knepley Level: intermediate 1525000e7ae3SMatthew Knepley 1526000e7ae3SMatthew Knepley .keywords: TS, timestep 1527000e7ae3SMatthew Knepley @*/ 15287087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1529000e7ae3SMatthew Knepley { 1530000e7ae3SMatthew Knepley PetscFunctionBegin; 15310700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1532000e7ae3SMatthew Knepley ts->ops->poststep = func; 1533000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1534000e7ae3SMatthew Knepley } 1535000e7ae3SMatthew Knepley 1536e74ef692SMatthew Knepley #undef __FUNCT__ 15373f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15383f2090d5SJed Brown /*@C 15393f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15403f2090d5SJed Brown 15413f2090d5SJed Brown Collective on TS 15423f2090d5SJed Brown 15433f2090d5SJed Brown Input Parameters: 15443f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15453f2090d5SJed Brown 15463f2090d5SJed Brown Notes: 15473f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15483f2090d5SJed Brown so most users would not generally call this routine themselves. 15493f2090d5SJed Brown 15503f2090d5SJed Brown Level: developer 15513f2090d5SJed Brown 15523f2090d5SJed Brown .keywords: TS, timestep 15533f2090d5SJed Brown @*/ 15547087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15553f2090d5SJed Brown { 15563f2090d5SJed Brown PetscErrorCode ierr; 15573f2090d5SJed Brown 15583f2090d5SJed Brown PetscFunctionBegin; 15590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 156072ac3e02SJed Brown if (ts->ops->poststep) { 15613f2090d5SJed Brown PetscStackPush("TS PostStep function"); 15623f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 15633f2090d5SJed Brown PetscStackPop; 156472ac3e02SJed Brown } 15653f2090d5SJed Brown PetscFunctionReturn(0); 15663f2090d5SJed Brown } 15673f2090d5SJed Brown 1568d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1569d763cef2SBarry Smith 15704a2ae208SSatish Balay #undef __FUNCT__ 1571a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1572d763cef2SBarry Smith /*@C 1573a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1574d763cef2SBarry Smith timestep to display the iteration's progress. 1575d763cef2SBarry Smith 15763f9fe445SBarry Smith Logically Collective on TS 1577d763cef2SBarry Smith 1578d763cef2SBarry Smith Input Parameters: 1579d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1580d763cef2SBarry Smith . func - monitoring routine 1581329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1582b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1583b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1584b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1585d763cef2SBarry Smith 1586d763cef2SBarry Smith Calling sequence of func: 1587a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1588d763cef2SBarry Smith 1589d763cef2SBarry Smith + ts - the TS context 1590d763cef2SBarry Smith . steps - iteration number 15911f06c33eSBarry Smith . time - current time 1592d763cef2SBarry Smith . x - current iterate 1593d763cef2SBarry Smith - mctx - [optional] monitoring context 1594d763cef2SBarry Smith 1595d763cef2SBarry Smith Notes: 1596d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1597d763cef2SBarry Smith already has been loaded. 1598d763cef2SBarry Smith 1599025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1600025f1a04SBarry Smith 1601d763cef2SBarry Smith Level: intermediate 1602d763cef2SBarry Smith 1603d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1604d763cef2SBarry Smith 1605a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1606d763cef2SBarry Smith @*/ 1607c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1608d763cef2SBarry Smith { 1609d763cef2SBarry Smith PetscFunctionBegin; 16100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 161117186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1612d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1613329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1614d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1615d763cef2SBarry Smith PetscFunctionReturn(0); 1616d763cef2SBarry Smith } 1617d763cef2SBarry Smith 16184a2ae208SSatish Balay #undef __FUNCT__ 1619a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1620d763cef2SBarry Smith /*@C 1621a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1622d763cef2SBarry Smith 16233f9fe445SBarry Smith Logically Collective on TS 1624d763cef2SBarry Smith 1625d763cef2SBarry Smith Input Parameters: 1626d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1627d763cef2SBarry Smith 1628d763cef2SBarry Smith Notes: 1629d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1630d763cef2SBarry Smith 1631d763cef2SBarry Smith Level: intermediate 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1634d763cef2SBarry Smith 1635a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1636d763cef2SBarry Smith @*/ 16377087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1638d763cef2SBarry Smith { 1639d952e501SBarry Smith PetscErrorCode ierr; 1640d952e501SBarry Smith PetscInt i; 1641d952e501SBarry Smith 1642d763cef2SBarry Smith PetscFunctionBegin; 16430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1644d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1645d952e501SBarry Smith if (ts->mdestroy[i]) { 16463c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1647d952e501SBarry Smith } 1648d952e501SBarry Smith } 1649d763cef2SBarry Smith ts->numbermonitors = 0; 1650d763cef2SBarry Smith PetscFunctionReturn(0); 1651d763cef2SBarry Smith } 1652d763cef2SBarry Smith 16534a2ae208SSatish Balay #undef __FUNCT__ 1654a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1655d8e5e3e6SSatish Balay /*@ 1656a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16575516499fSSatish Balay 16585516499fSSatish Balay Level: intermediate 165941251cbbSSatish Balay 16605516499fSSatish Balay .keywords: TS, set, monitor 16615516499fSSatish Balay 166241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 166341251cbbSSatish Balay @*/ 1664649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1665d763cef2SBarry Smith { 1666dfbe8321SBarry Smith PetscErrorCode ierr; 1667649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1668d132466eSBarry Smith 1669d763cef2SBarry Smith PetscFunctionBegin; 1670649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1671649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1672649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1673d763cef2SBarry Smith PetscFunctionReturn(0); 1674d763cef2SBarry Smith } 1675d763cef2SBarry Smith 16764a2ae208SSatish Balay #undef __FUNCT__ 16774a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1678d763cef2SBarry Smith /*@ 1679d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith Collective on TS 1682d763cef2SBarry Smith 1683d763cef2SBarry Smith Input Parameter: 1684d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1685d763cef2SBarry Smith 1686d763cef2SBarry Smith Output Parameters: 1687d763cef2SBarry Smith + steps - number of iterations until termination 1688142b95e3SSatish Balay - ptime - time until termination 1689d763cef2SBarry Smith 1690d763cef2SBarry Smith Level: beginner 1691d763cef2SBarry Smith 1692d763cef2SBarry Smith .keywords: TS, timestep, solve 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1695d763cef2SBarry Smith @*/ 1696193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1697d763cef2SBarry Smith { 1698dfbe8321SBarry Smith PetscErrorCode ierr; 1699d763cef2SBarry Smith 1700d763cef2SBarry Smith PetscFunctionBegin; 17010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1702277b19d0SLisandro Dalcin 1703d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1704d405a339SMatthew Knepley 1705d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1706193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1707d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1708d763cef2SBarry Smith PetscFunctionReturn(0); 1709d763cef2SBarry Smith } 1710d763cef2SBarry Smith 17114a2ae208SSatish Balay #undef __FUNCT__ 17126a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17136a4d4014SLisandro Dalcin /*@ 17146a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17156a4d4014SLisandro Dalcin 17166a4d4014SLisandro Dalcin Collective on TS 17176a4d4014SLisandro Dalcin 17186a4d4014SLisandro Dalcin Input Parameter: 17196a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17206a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17216a4d4014SLisandro Dalcin 17226a4d4014SLisandro Dalcin Level: beginner 17236a4d4014SLisandro Dalcin 17246a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17256a4d4014SLisandro Dalcin 17266a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17276a4d4014SLisandro Dalcin @*/ 17287087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17296a4d4014SLisandro Dalcin { 1730193ac0bcSJed Brown PetscInt i; 17316a4d4014SLisandro Dalcin PetscErrorCode ierr; 1732f22f69f0SBarry Smith 17336a4d4014SLisandro Dalcin PetscFunctionBegin; 17340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1735193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1736193ac0bcSJed Brown ierr = TSSetSolution(ts,x); CHKERRQ(ierr); 1737b5d403baSSean Farley ierr = TSSetUp(ts); CHKERRQ(ierr); 17386a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1739193ac0bcSJed Brown ts->steps = 0; 1740193ac0bcSJed Brown ts->linear_its = 0; 1741193ac0bcSJed Brown ts->nonlinear_its = 0; 1742193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1743193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1744193ac0bcSJed Brown 1745193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1746193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 1747193ac0bcSJed Brown } else { 17486a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 17493daf2de8SJed Brown for (i=0; !ts->reason; ) { 1750193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1751193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1752193ac0bcSJed Brown if (ts->reason < 0) { 1753193ac0bcSJed Brown if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed"); 17543daf2de8SJed Brown } else if (++i >= ts->max_steps) { 1755193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITS; 1756193ac0bcSJed Brown } else if (ts->ptime >= ts->max_time) { 1757193ac0bcSJed Brown ts->reason = TS_CONVERGED_TIME; 1758193ac0bcSJed Brown } 1759193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1760193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1761193ac0bcSJed Brown } 1762193ac0bcSJed Brown } 1763193ac0bcSJed Brown if (!PetscPreLoadingOn) { 1764193ac0bcSJed Brown ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1765193ac0bcSJed Brown } 17666a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17676a4d4014SLisandro Dalcin } 17686a4d4014SLisandro Dalcin 17696a4d4014SLisandro Dalcin #undef __FUNCT__ 17704a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1771d763cef2SBarry Smith /* 1772d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1773d763cef2SBarry Smith */ 1774a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1775d763cef2SBarry Smith { 17766849ba73SBarry Smith PetscErrorCode ierr; 1777a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1778d763cef2SBarry Smith 1779d763cef2SBarry Smith PetscFunctionBegin; 1780d763cef2SBarry Smith for (i=0; i<n; i++) { 1781142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1782d763cef2SBarry Smith } 1783d763cef2SBarry Smith PetscFunctionReturn(0); 1784d763cef2SBarry Smith } 1785d763cef2SBarry Smith 1786d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1787d763cef2SBarry Smith 17884a2ae208SSatish Balay #undef __FUNCT__ 1789a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1790d763cef2SBarry Smith /*@C 1791a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1792d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1793d763cef2SBarry Smith 1794d763cef2SBarry Smith Collective on TS 1795d763cef2SBarry Smith 1796d763cef2SBarry Smith Input Parameters: 1797d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1798d763cef2SBarry Smith . label - the title to put in the title bar 17997c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1800d763cef2SBarry Smith - m, n - the screen width and height in pixels 1801d763cef2SBarry Smith 1802d763cef2SBarry Smith Output Parameter: 1803d763cef2SBarry Smith . draw - the drawing context 1804d763cef2SBarry Smith 1805d763cef2SBarry Smith Options Database Key: 1806a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1807d763cef2SBarry Smith 1808d763cef2SBarry Smith Notes: 1809a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1810d763cef2SBarry Smith 1811d763cef2SBarry Smith Level: intermediate 1812d763cef2SBarry Smith 18137c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1814d763cef2SBarry Smith 1815a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18167c922b88SBarry Smith 1817d763cef2SBarry Smith @*/ 18187087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1819d763cef2SBarry Smith { 1820b0a32e0cSBarry Smith PetscDraw win; 1821dfbe8321SBarry Smith PetscErrorCode ierr; 1822d763cef2SBarry Smith 1823d763cef2SBarry Smith PetscFunctionBegin; 1824b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1825b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1826b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1827b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1828d763cef2SBarry Smith 182952e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1830d763cef2SBarry Smith PetscFunctionReturn(0); 1831d763cef2SBarry Smith } 1832d763cef2SBarry Smith 18334a2ae208SSatish Balay #undef __FUNCT__ 1834a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1835a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1836d763cef2SBarry Smith { 1837b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 183887828ca2SBarry Smith PetscReal x,y = ptime; 1839dfbe8321SBarry Smith PetscErrorCode ierr; 1840d763cef2SBarry Smith 1841d763cef2SBarry Smith PetscFunctionBegin; 18427c922b88SBarry Smith if (!monctx) { 18437c922b88SBarry Smith MPI_Comm comm; 1844b0a32e0cSBarry Smith PetscViewer viewer; 18457c922b88SBarry Smith 18467c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1847b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1848b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18497c922b88SBarry Smith } 18507c922b88SBarry Smith 1851b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 185287828ca2SBarry Smith x = (PetscReal)n; 1853b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1854d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1855b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1856d763cef2SBarry Smith } 1857d763cef2SBarry Smith PetscFunctionReturn(0); 1858d763cef2SBarry Smith } 1859d763cef2SBarry Smith 18604a2ae208SSatish Balay #undef __FUNCT__ 1861a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1862d763cef2SBarry Smith /*@C 1863a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1864a6570f20SBarry Smith with TSMonitorLGCreate(). 1865d763cef2SBarry Smith 1866b0a32e0cSBarry Smith Collective on PetscDrawLG 1867d763cef2SBarry Smith 1868d763cef2SBarry Smith Input Parameter: 1869d763cef2SBarry Smith . draw - the drawing context 1870d763cef2SBarry Smith 1871d763cef2SBarry Smith Level: intermediate 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1874d763cef2SBarry Smith 1875a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1876d763cef2SBarry Smith @*/ 18776bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1878d763cef2SBarry Smith { 1879b0a32e0cSBarry Smith PetscDraw draw; 1880dfbe8321SBarry Smith PetscErrorCode ierr; 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith PetscFunctionBegin; 18836bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 18846bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1885b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1886d763cef2SBarry Smith PetscFunctionReturn(0); 1887d763cef2SBarry Smith } 1888d763cef2SBarry Smith 18894a2ae208SSatish Balay #undef __FUNCT__ 18904a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1891d763cef2SBarry Smith /*@ 1892d763cef2SBarry Smith TSGetTime - Gets the current time. 1893d763cef2SBarry Smith 1894d763cef2SBarry Smith Not Collective 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith Input Parameter: 1897d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1898d763cef2SBarry Smith 1899d763cef2SBarry Smith Output Parameter: 1900d763cef2SBarry Smith . t - the current time 1901d763cef2SBarry Smith 1902d763cef2SBarry Smith Level: beginner 1903d763cef2SBarry Smith 1904d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1905d763cef2SBarry Smith 1906d763cef2SBarry Smith .keywords: TS, get, time 1907d763cef2SBarry Smith @*/ 19087087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1909d763cef2SBarry Smith { 1910d763cef2SBarry Smith PetscFunctionBegin; 19110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19124482741eSBarry Smith PetscValidDoublePointer(t,2); 1913d763cef2SBarry Smith *t = ts->ptime; 1914d763cef2SBarry Smith PetscFunctionReturn(0); 1915d763cef2SBarry Smith } 1916d763cef2SBarry Smith 19174a2ae208SSatish Balay #undef __FUNCT__ 19186a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19196a4d4014SLisandro Dalcin /*@ 19206a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19216a4d4014SLisandro Dalcin 19223f9fe445SBarry Smith Logically Collective on TS 19236a4d4014SLisandro Dalcin 19246a4d4014SLisandro Dalcin Input Parameters: 19256a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19266a4d4014SLisandro Dalcin - time - the time 19276a4d4014SLisandro Dalcin 19286a4d4014SLisandro Dalcin Level: intermediate 19296a4d4014SLisandro Dalcin 19306a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19316a4d4014SLisandro Dalcin 19326a4d4014SLisandro Dalcin .keywords: TS, set, time 19336a4d4014SLisandro Dalcin @*/ 19347087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19356a4d4014SLisandro Dalcin { 19366a4d4014SLisandro Dalcin PetscFunctionBegin; 19370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1938c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19396a4d4014SLisandro Dalcin ts->ptime = t; 19406a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19416a4d4014SLisandro Dalcin } 19426a4d4014SLisandro Dalcin 19436a4d4014SLisandro Dalcin #undef __FUNCT__ 19444a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1945d763cef2SBarry Smith /*@C 1946d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1947d763cef2SBarry Smith TS options in the database. 1948d763cef2SBarry Smith 19493f9fe445SBarry Smith Logically Collective on TS 1950d763cef2SBarry Smith 1951d763cef2SBarry Smith Input Parameter: 1952d763cef2SBarry Smith + ts - The TS context 1953d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1954d763cef2SBarry Smith 1955d763cef2SBarry Smith Notes: 1956d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1957d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1958d763cef2SBarry Smith hyphen. 1959d763cef2SBarry Smith 1960d763cef2SBarry Smith Level: advanced 1961d763cef2SBarry Smith 1962d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1963d763cef2SBarry Smith 1964d763cef2SBarry Smith .seealso: TSSetFromOptions() 1965d763cef2SBarry Smith 1966d763cef2SBarry Smith @*/ 19677087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1968d763cef2SBarry Smith { 1969dfbe8321SBarry Smith PetscErrorCode ierr; 1970089b2837SJed Brown SNES snes; 1971d763cef2SBarry Smith 1972d763cef2SBarry Smith PetscFunctionBegin; 19730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1974d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1975089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1976089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 1977d763cef2SBarry Smith PetscFunctionReturn(0); 1978d763cef2SBarry Smith } 1979d763cef2SBarry Smith 1980d763cef2SBarry Smith 19814a2ae208SSatish Balay #undef __FUNCT__ 19824a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 1983d763cef2SBarry Smith /*@C 1984d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 1985d763cef2SBarry Smith TS options in the database. 1986d763cef2SBarry Smith 19873f9fe445SBarry Smith Logically Collective on TS 1988d763cef2SBarry Smith 1989d763cef2SBarry Smith Input Parameter: 1990d763cef2SBarry Smith + ts - The TS context 1991d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1992d763cef2SBarry Smith 1993d763cef2SBarry Smith Notes: 1994d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1995d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1996d763cef2SBarry Smith hyphen. 1997d763cef2SBarry Smith 1998d763cef2SBarry Smith Level: advanced 1999d763cef2SBarry Smith 2000d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2001d763cef2SBarry Smith 2002d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2003d763cef2SBarry Smith 2004d763cef2SBarry Smith @*/ 20057087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2006d763cef2SBarry Smith { 2007dfbe8321SBarry Smith PetscErrorCode ierr; 2008089b2837SJed Brown SNES snes; 2009d763cef2SBarry Smith 2010d763cef2SBarry Smith PetscFunctionBegin; 20110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2012d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2013089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2014089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2015d763cef2SBarry Smith PetscFunctionReturn(0); 2016d763cef2SBarry Smith } 2017d763cef2SBarry Smith 20184a2ae208SSatish Balay #undef __FUNCT__ 20194a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2020d763cef2SBarry Smith /*@C 2021d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2022d763cef2SBarry Smith TS options in the database. 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith Not Collective 2025d763cef2SBarry Smith 2026d763cef2SBarry Smith Input Parameter: 2027d763cef2SBarry Smith . ts - The TS context 2028d763cef2SBarry Smith 2029d763cef2SBarry Smith Output Parameter: 2030d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2031d763cef2SBarry Smith 2032d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2033d763cef2SBarry Smith sufficient length to hold the prefix. 2034d763cef2SBarry Smith 2035d763cef2SBarry Smith Level: intermediate 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2038d763cef2SBarry Smith 2039d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2040d763cef2SBarry Smith @*/ 20417087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2042d763cef2SBarry Smith { 2043dfbe8321SBarry Smith PetscErrorCode ierr; 2044d763cef2SBarry Smith 2045d763cef2SBarry Smith PetscFunctionBegin; 20460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20474482741eSBarry Smith PetscValidPointer(prefix,2); 2048d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2049d763cef2SBarry Smith PetscFunctionReturn(0); 2050d763cef2SBarry Smith } 2051d763cef2SBarry Smith 20524a2ae208SSatish Balay #undef __FUNCT__ 20534a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2054d763cef2SBarry Smith /*@C 2055d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith Input Parameter: 2060d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith Output Parameters: 2063d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2064d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2065089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2066d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2067d763cef2SBarry Smith 2068d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith Level: intermediate 2071d763cef2SBarry Smith 207226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2073d763cef2SBarry Smith 2074d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2075d763cef2SBarry Smith @*/ 2076089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2077d763cef2SBarry Smith { 2078089b2837SJed Brown PetscErrorCode ierr; 2079089b2837SJed Brown SNES snes; 2080089b2837SJed Brown 2081d763cef2SBarry Smith PetscFunctionBegin; 2082089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2083089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 20843b5f76d0SSean Farley if (func) *func = ts->userops->rhsjacobian; 208526d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2086d763cef2SBarry Smith PetscFunctionReturn(0); 2087d763cef2SBarry Smith } 2088d763cef2SBarry Smith 20891713a123SBarry Smith #undef __FUNCT__ 20902eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 20912eca1d9cSJed Brown /*@C 20922eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 20932eca1d9cSJed Brown 20942eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 20952eca1d9cSJed Brown 20962eca1d9cSJed Brown Input Parameter: 20972eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 20982eca1d9cSJed Brown 20992eca1d9cSJed Brown Output Parameters: 21002eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 21012eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 21022eca1d9cSJed Brown . f - The function to compute the matrices 21032eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 21042eca1d9cSJed Brown 21052eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 21062eca1d9cSJed Brown 21072eca1d9cSJed Brown Level: advanced 21082eca1d9cSJed Brown 21092eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 21102eca1d9cSJed Brown 21112eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 21122eca1d9cSJed Brown @*/ 21137087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21142eca1d9cSJed Brown { 2115089b2837SJed Brown PetscErrorCode ierr; 2116089b2837SJed Brown SNES snes; 2117089b2837SJed Brown 21182eca1d9cSJed Brown PetscFunctionBegin; 2119089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2120089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21213b5f76d0SSean Farley if (f) *f = ts->userops->ijacobian; 21222eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21232eca1d9cSJed Brown PetscFunctionReturn(0); 21242eca1d9cSJed Brown } 21252eca1d9cSJed Brown 21262eca1d9cSJed Brown #undef __FUNCT__ 2127a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21281713a123SBarry Smith /*@C 2129a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21301713a123SBarry Smith VecView() for the solution at each timestep 21311713a123SBarry Smith 21321713a123SBarry Smith Collective on TS 21331713a123SBarry Smith 21341713a123SBarry Smith Input Parameters: 21351713a123SBarry Smith + ts - the TS context 21361713a123SBarry Smith . step - current time-step 2137142b95e3SSatish Balay . ptime - current time 21381713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21391713a123SBarry Smith 21401713a123SBarry Smith Level: intermediate 21411713a123SBarry Smith 21421713a123SBarry Smith .keywords: TS, vector, monitor, view 21431713a123SBarry Smith 2144a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21451713a123SBarry Smith @*/ 21467087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21471713a123SBarry Smith { 2148dfbe8321SBarry Smith PetscErrorCode ierr; 21491713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21501713a123SBarry Smith 21511713a123SBarry Smith PetscFunctionBegin; 2152a34d58ebSBarry Smith if (!dummy) { 21537adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21541713a123SBarry Smith } 21551713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21561713a123SBarry Smith PetscFunctionReturn(0); 21571713a123SBarry Smith } 21581713a123SBarry Smith 21591713a123SBarry Smith 21606c699258SBarry Smith #undef __FUNCT__ 21616c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21626c699258SBarry Smith /*@ 21636c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21646c699258SBarry Smith 21653f9fe445SBarry Smith Logically Collective on TS and DM 21666c699258SBarry Smith 21676c699258SBarry Smith Input Parameters: 21686c699258SBarry Smith + ts - the preconditioner context 21696c699258SBarry Smith - dm - the dm 21706c699258SBarry Smith 21716c699258SBarry Smith Level: intermediate 21726c699258SBarry Smith 21736c699258SBarry Smith 21746c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 21756c699258SBarry Smith @*/ 21767087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 21776c699258SBarry Smith { 21786c699258SBarry Smith PetscErrorCode ierr; 2179089b2837SJed Brown SNES snes; 21806c699258SBarry Smith 21816c699258SBarry Smith PetscFunctionBegin; 21820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 218370663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 21846bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 21856c699258SBarry Smith ts->dm = dm; 2186089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2187089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 21886c699258SBarry Smith PetscFunctionReturn(0); 21896c699258SBarry Smith } 21906c699258SBarry Smith 21916c699258SBarry Smith #undef __FUNCT__ 21926c699258SBarry Smith #define __FUNCT__ "TSGetDM" 21936c699258SBarry Smith /*@ 21946c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 21956c699258SBarry Smith 21963f9fe445SBarry Smith Not Collective 21976c699258SBarry Smith 21986c699258SBarry Smith Input Parameter: 21996c699258SBarry Smith . ts - the preconditioner context 22006c699258SBarry Smith 22016c699258SBarry Smith Output Parameter: 22026c699258SBarry Smith . dm - the dm 22036c699258SBarry Smith 22046c699258SBarry Smith Level: intermediate 22056c699258SBarry Smith 22066c699258SBarry Smith 22076c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 22086c699258SBarry Smith @*/ 22097087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 22106c699258SBarry Smith { 22116c699258SBarry Smith PetscFunctionBegin; 22120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22136c699258SBarry Smith *dm = ts->dm; 22146c699258SBarry Smith PetscFunctionReturn(0); 22156c699258SBarry Smith } 22161713a123SBarry Smith 22170f5c6efeSJed Brown #undef __FUNCT__ 22180f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22190f5c6efeSJed Brown /*@ 22200f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22210f5c6efeSJed Brown 22223f9fe445SBarry Smith Logically Collective on SNES 22230f5c6efeSJed Brown 22240f5c6efeSJed Brown Input Parameter: 2225d42a1c89SJed Brown + snes - nonlinear solver 22260f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2227d42a1c89SJed Brown - ctx - user context, must be a TS 22280f5c6efeSJed Brown 22290f5c6efeSJed Brown Output Parameter: 22300f5c6efeSJed Brown . F - the nonlinear residual 22310f5c6efeSJed Brown 22320f5c6efeSJed Brown Notes: 22330f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22340f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22350f5c6efeSJed Brown 22360f5c6efeSJed Brown Level: advanced 22370f5c6efeSJed Brown 22380f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22390f5c6efeSJed Brown @*/ 22407087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22410f5c6efeSJed Brown { 22420f5c6efeSJed Brown TS ts = (TS)ctx; 22430f5c6efeSJed Brown PetscErrorCode ierr; 22440f5c6efeSJed Brown 22450f5c6efeSJed Brown PetscFunctionBegin; 22460f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22470f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22480f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22490f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22500f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22510f5c6efeSJed Brown PetscFunctionReturn(0); 22520f5c6efeSJed Brown } 22530f5c6efeSJed Brown 22540f5c6efeSJed Brown #undef __FUNCT__ 22550f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22560f5c6efeSJed Brown /*@ 22570f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22580f5c6efeSJed Brown 22590f5c6efeSJed Brown Collective on SNES 22600f5c6efeSJed Brown 22610f5c6efeSJed Brown Input Parameter: 22620f5c6efeSJed Brown + snes - nonlinear solver 22630f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22640f5c6efeSJed Brown - ctx - user context, must be a TS 22650f5c6efeSJed Brown 22660f5c6efeSJed Brown Output Parameter: 22670f5c6efeSJed Brown + A - the Jacobian 22680f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 22690f5c6efeSJed Brown - flag - indicates any structure change in the matrix 22700f5c6efeSJed Brown 22710f5c6efeSJed Brown Notes: 22720f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22730f5c6efeSJed Brown 22740f5c6efeSJed Brown Level: developer 22750f5c6efeSJed Brown 22760f5c6efeSJed Brown .seealso: SNESSetJacobian() 22770f5c6efeSJed Brown @*/ 22787087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 22790f5c6efeSJed Brown { 22800f5c6efeSJed Brown TS ts = (TS)ctx; 22810f5c6efeSJed Brown PetscErrorCode ierr; 22820f5c6efeSJed Brown 22830f5c6efeSJed Brown PetscFunctionBegin; 22840f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22850f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22860f5c6efeSJed Brown PetscValidPointer(A,3); 22870f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 22880f5c6efeSJed Brown PetscValidPointer(B,4); 22890f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 22900f5c6efeSJed Brown PetscValidPointer(flag,5); 22910f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 22920f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 22930f5c6efeSJed Brown PetscFunctionReturn(0); 22940f5c6efeSJed Brown } 2295325fc9f4SBarry Smith 22960e4ef248SJed Brown #undef __FUNCT__ 22970e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 22980e4ef248SJed Brown /*@C 22990e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 23000e4ef248SJed Brown 23010e4ef248SJed Brown Collective on TS 23020e4ef248SJed Brown 23030e4ef248SJed Brown Input Arguments: 23040e4ef248SJed Brown + ts - time stepping context 23050e4ef248SJed Brown . t - time at which to evaluate 23060e4ef248SJed Brown . X - state at which to evaluate 23070e4ef248SJed Brown - ctx - context 23080e4ef248SJed Brown 23090e4ef248SJed Brown Output Arguments: 23100e4ef248SJed Brown . F - right hand side 23110e4ef248SJed Brown 23120e4ef248SJed Brown Level: intermediate 23130e4ef248SJed Brown 23140e4ef248SJed Brown Notes: 23150e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 23160e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 23170e4ef248SJed Brown 23180e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 23190e4ef248SJed Brown @*/ 23200e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 23210e4ef248SJed Brown { 23220e4ef248SJed Brown PetscErrorCode ierr; 23230e4ef248SJed Brown Mat Arhs,Brhs; 23240e4ef248SJed Brown MatStructure flg2; 23250e4ef248SJed Brown 23260e4ef248SJed Brown PetscFunctionBegin; 23270e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 23280e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 23290e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 23300e4ef248SJed Brown PetscFunctionReturn(0); 23310e4ef248SJed Brown } 23320e4ef248SJed Brown 23330e4ef248SJed Brown #undef __FUNCT__ 23340e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 23350e4ef248SJed Brown /*@C 23360e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 23370e4ef248SJed Brown 23380e4ef248SJed Brown Collective on TS 23390e4ef248SJed Brown 23400e4ef248SJed Brown Input Arguments: 23410e4ef248SJed Brown + ts - time stepping context 23420e4ef248SJed Brown . t - time at which to evaluate 23430e4ef248SJed Brown . X - state at which to evaluate 23440e4ef248SJed Brown - ctx - context 23450e4ef248SJed Brown 23460e4ef248SJed Brown Output Arguments: 23470e4ef248SJed Brown + A - pointer to operator 23480e4ef248SJed Brown . B - pointer to preconditioning matrix 23490e4ef248SJed Brown - flg - matrix structure flag 23500e4ef248SJed Brown 23510e4ef248SJed Brown Level: intermediate 23520e4ef248SJed Brown 23530e4ef248SJed Brown Notes: 23540e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 23550e4ef248SJed Brown 23560e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 23570e4ef248SJed Brown @*/ 23580e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 23590e4ef248SJed Brown { 23600e4ef248SJed Brown 23610e4ef248SJed Brown PetscFunctionBegin; 23620e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 23630e4ef248SJed Brown PetscFunctionReturn(0); 23640e4ef248SJed Brown } 23650e4ef248SJed Brown 2366325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2367c6db04a5SJed Brown #include <mex.h> 2368325fc9f4SBarry Smith 2369325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2370325fc9f4SBarry Smith 2371325fc9f4SBarry Smith #undef __FUNCT__ 2372325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2373325fc9f4SBarry Smith /* 2374325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2375325fc9f4SBarry Smith TSSetFunctionMatlab(). 2376325fc9f4SBarry Smith 2377325fc9f4SBarry Smith Collective on TS 2378325fc9f4SBarry Smith 2379325fc9f4SBarry Smith Input Parameters: 2380325fc9f4SBarry Smith + snes - the TS context 2381325fc9f4SBarry Smith - x - input vector 2382325fc9f4SBarry Smith 2383325fc9f4SBarry Smith Output Parameter: 2384325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2385325fc9f4SBarry Smith 2386325fc9f4SBarry Smith Notes: 2387325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2388325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2389325fc9f4SBarry Smith themselves. 2390325fc9f4SBarry Smith 2391325fc9f4SBarry Smith Level: developer 2392325fc9f4SBarry Smith 2393325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2394325fc9f4SBarry Smith 2395325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2396325fc9f4SBarry Smith */ 23977087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2398325fc9f4SBarry Smith { 2399325fc9f4SBarry Smith PetscErrorCode ierr; 2400325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2401325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2402325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2403325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2404325fc9f4SBarry Smith 2405325fc9f4SBarry Smith PetscFunctionBegin; 2406325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2407325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2408325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2409325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2410325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2411325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2412325fc9f4SBarry Smith 2413325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2414325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2415880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2416325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2417325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2418325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2419325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2420325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2421325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2422325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2423325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2424325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2425325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2426325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2427325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2428325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2429325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2430325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2431325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2432325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2433325fc9f4SBarry Smith PetscFunctionReturn(0); 2434325fc9f4SBarry Smith } 2435325fc9f4SBarry Smith 2436325fc9f4SBarry Smith 2437325fc9f4SBarry Smith #undef __FUNCT__ 2438325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2439325fc9f4SBarry Smith /* 2440325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2441325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2442e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2443325fc9f4SBarry Smith 2444325fc9f4SBarry Smith Logically Collective on TS 2445325fc9f4SBarry Smith 2446325fc9f4SBarry Smith Input Parameters: 2447325fc9f4SBarry Smith + ts - the TS context 2448325fc9f4SBarry Smith - func - function evaluation routine 2449325fc9f4SBarry Smith 2450325fc9f4SBarry Smith Calling sequence of func: 2451325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2452325fc9f4SBarry Smith 2453325fc9f4SBarry Smith Level: beginner 2454325fc9f4SBarry Smith 2455325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2456325fc9f4SBarry Smith 2457325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2458325fc9f4SBarry Smith */ 24597087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2460325fc9f4SBarry Smith { 2461325fc9f4SBarry Smith PetscErrorCode ierr; 2462325fc9f4SBarry Smith TSMatlabContext *sctx; 2463325fc9f4SBarry Smith 2464325fc9f4SBarry Smith PetscFunctionBegin; 2465325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2466325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2467325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2468325fc9f4SBarry Smith /* 2469325fc9f4SBarry Smith This should work, but it doesn't 2470325fc9f4SBarry Smith sctx->ctx = ctx; 2471325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2472325fc9f4SBarry Smith */ 2473325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2474325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2475325fc9f4SBarry Smith PetscFunctionReturn(0); 2476325fc9f4SBarry Smith } 2477325fc9f4SBarry Smith 2478325fc9f4SBarry Smith #undef __FUNCT__ 2479325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2480325fc9f4SBarry Smith /* 2481325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2482325fc9f4SBarry Smith TSSetJacobianMatlab(). 2483325fc9f4SBarry Smith 2484325fc9f4SBarry Smith Collective on TS 2485325fc9f4SBarry Smith 2486325fc9f4SBarry Smith Input Parameters: 2487325fc9f4SBarry Smith + snes - the TS context 2488325fc9f4SBarry Smith . x - input vector 2489325fc9f4SBarry Smith . A, B - the matrices 2490325fc9f4SBarry Smith - ctx - user context 2491325fc9f4SBarry Smith 2492325fc9f4SBarry Smith Output Parameter: 2493325fc9f4SBarry Smith . flag - structure of the matrix 2494325fc9f4SBarry Smith 2495325fc9f4SBarry Smith Level: developer 2496325fc9f4SBarry Smith 2497325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2498325fc9f4SBarry Smith 2499325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2500325fc9f4SBarry Smith @*/ 25017087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2502325fc9f4SBarry Smith { 2503325fc9f4SBarry Smith PetscErrorCode ierr; 2504325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2505325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2506325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2507325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2508325fc9f4SBarry Smith 2509325fc9f4SBarry Smith PetscFunctionBegin; 2510325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2511325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2512325fc9f4SBarry Smith 2513325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2514325fc9f4SBarry Smith 2515325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2516325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2517325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2518325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2519325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2520325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2521325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2522325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2523325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2524325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2525325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2526325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2527325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2528325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2529325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2530325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2531325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2532325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2533325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2534325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2535325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2536325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2537325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2538325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2539325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2540325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2541325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2542325fc9f4SBarry Smith PetscFunctionReturn(0); 2543325fc9f4SBarry Smith } 2544325fc9f4SBarry Smith 2545325fc9f4SBarry Smith 2546325fc9f4SBarry Smith #undef __FUNCT__ 2547325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2548325fc9f4SBarry Smith /* 2549325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2550e3c5b3baSBarry 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 2551325fc9f4SBarry Smith 2552325fc9f4SBarry Smith Logically Collective on TS 2553325fc9f4SBarry Smith 2554325fc9f4SBarry Smith Input Parameters: 2555325fc9f4SBarry Smith + snes - the TS context 2556325fc9f4SBarry Smith . A,B - Jacobian matrices 2557325fc9f4SBarry Smith . func - function evaluation routine 2558325fc9f4SBarry Smith - ctx - user context 2559325fc9f4SBarry Smith 2560325fc9f4SBarry Smith Calling sequence of func: 2561325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2562325fc9f4SBarry Smith 2563325fc9f4SBarry Smith 2564325fc9f4SBarry Smith Level: developer 2565325fc9f4SBarry Smith 2566325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2567325fc9f4SBarry Smith 2568325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2569325fc9f4SBarry Smith */ 25707087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2571325fc9f4SBarry Smith { 2572325fc9f4SBarry Smith PetscErrorCode ierr; 2573325fc9f4SBarry Smith TSMatlabContext *sctx; 2574325fc9f4SBarry Smith 2575325fc9f4SBarry Smith PetscFunctionBegin; 2576325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2577325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2578325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2579325fc9f4SBarry Smith /* 2580325fc9f4SBarry Smith This should work, but it doesn't 2581325fc9f4SBarry Smith sctx->ctx = ctx; 2582325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2583325fc9f4SBarry Smith */ 2584325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2585325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2586325fc9f4SBarry Smith PetscFunctionReturn(0); 2587325fc9f4SBarry Smith } 2588325fc9f4SBarry Smith 2589b5b1a830SBarry Smith #undef __FUNCT__ 2590b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2591b5b1a830SBarry Smith /* 2592b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2593b5b1a830SBarry Smith 2594b5b1a830SBarry Smith Collective on TS 2595b5b1a830SBarry Smith 2596b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2597b5b1a830SBarry Smith @*/ 25987087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2599b5b1a830SBarry Smith { 2600b5b1a830SBarry Smith PetscErrorCode ierr; 2601b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2602a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2603b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2604b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2605b5b1a830SBarry Smith 2606b5b1a830SBarry Smith PetscFunctionBegin; 2607b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2608b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2609b5b1a830SBarry Smith 2610b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2611b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2612b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2613b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2614b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2615b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2616b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2617b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2618b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2619b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2620b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2621b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2622b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2623b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2624b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2625b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2626b5b1a830SBarry Smith PetscFunctionReturn(0); 2627b5b1a830SBarry Smith } 2628b5b1a830SBarry Smith 2629b5b1a830SBarry Smith 2630b5b1a830SBarry Smith #undef __FUNCT__ 2631b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2632b5b1a830SBarry Smith /* 2633b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2634b5b1a830SBarry Smith 2635b5b1a830SBarry Smith Level: developer 2636b5b1a830SBarry Smith 2637b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2638b5b1a830SBarry Smith 2639b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2640b5b1a830SBarry Smith */ 26417087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2642b5b1a830SBarry Smith { 2643b5b1a830SBarry Smith PetscErrorCode ierr; 2644b5b1a830SBarry Smith TSMatlabContext *sctx; 2645b5b1a830SBarry Smith 2646b5b1a830SBarry Smith PetscFunctionBegin; 2647b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2648b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2649b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2650b5b1a830SBarry Smith /* 2651b5b1a830SBarry Smith This should work, but it doesn't 2652b5b1a830SBarry Smith sctx->ctx = ctx; 2653b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2654b5b1a830SBarry Smith */ 2655b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2656b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2657b5b1a830SBarry Smith PetscFunctionReturn(0); 2658b5b1a830SBarry Smith } 2659b5b1a830SBarry Smith 2660325fc9f4SBarry Smith #endif 2661