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 } 1005180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1015180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1025180491cSLisandro Dalcin 10390d69ab7SBarry Smith opt = PETSC_FALSE; 104acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 105a7cc72afSBarry Smith if (opt) { 106a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 107bdad233fSMatthew Knepley } 10890d69ab7SBarry Smith opt = PETSC_FALSE; 109acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 110a7cc72afSBarry Smith if (opt) { 111a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 112bdad233fSMatthew Knepley } 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley /* Handle TS type options */ 115bdad233fSMatthew Knepley ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 116bdad233fSMatthew Knepley 117bdad233fSMatthew Knepley /* Handle specific TS options */ 118abc0a331SBarry Smith if (ts->ops->setfromoptions) { 119bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 120bdad233fSMatthew Knepley } 1215d973c19SBarry Smith 1225d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1235d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 124bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 125bdad233fSMatthew Knepley 126089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 127bdad233fSMatthew Knepley /* Handle subobject options */ 128089b2837SJed Brown if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 129089b2837SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 130bdad233fSMatthew Knepley PetscFunctionReturn(0); 131bdad233fSMatthew Knepley } 132bdad233fSMatthew Knepley 133bdad233fSMatthew Knepley #undef __FUNCT__ 134*cdcf91faSSean Farley #undef __FUNCT__ 1354a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 136a7a1495cSBarry Smith /*@ 1378c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 138a7a1495cSBarry Smith set with TSSetRHSJacobian(). 139a7a1495cSBarry Smith 140a7a1495cSBarry Smith Collective on TS and Vec 141a7a1495cSBarry Smith 142a7a1495cSBarry Smith Input Parameters: 143316643e7SJed Brown + ts - the TS context 144a7a1495cSBarry Smith . t - current timestep 145a7a1495cSBarry Smith - x - input vector 146a7a1495cSBarry Smith 147a7a1495cSBarry Smith Output Parameters: 148a7a1495cSBarry Smith + A - Jacobian matrix 149a7a1495cSBarry Smith . B - optional preconditioning matrix 150a7a1495cSBarry Smith - flag - flag indicating matrix structure 151a7a1495cSBarry Smith 152a7a1495cSBarry Smith Notes: 153a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 154a7a1495cSBarry Smith is used internally within the nonlinear solvers. 155a7a1495cSBarry Smith 15694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 157a7a1495cSBarry Smith flag parameter. 158a7a1495cSBarry Smith 159a7a1495cSBarry Smith Level: developer 160a7a1495cSBarry Smith 161a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 162a7a1495cSBarry Smith 16394b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 164a7a1495cSBarry Smith @*/ 1657087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 166a7a1495cSBarry Smith { 167dfbe8321SBarry Smith PetscErrorCode ierr; 1680e4ef248SJed Brown PetscInt Xstate; 169a7a1495cSBarry Smith 170a7a1495cSBarry Smith PetscFunctionBegin; 1710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1720700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 173c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 1740e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 1750e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 1760e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 1770e4ef248SJed Brown PetscFunctionReturn(0); 178f8ede8e7SJed Brown } 179d90be118SSean Farley 1803b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 181d90be118SSean Farley 1823b5f76d0SSean Farley if (ts->userops->rhsjacobian) { 183d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 184a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 185a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 1863b5f76d0SSean Farley ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 187a7a1495cSBarry Smith PetscStackPop; 188d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 189a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 1900700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 1910700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 192ef66eb69SBarry Smith } else { 193214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 194214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 195214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 196ef66eb69SBarry Smith } 1970e4ef248SJed Brown ts->rhsjacobian.time = t; 1980e4ef248SJed Brown ts->rhsjacobian.X = X; 1990e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2000e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 201a7a1495cSBarry Smith PetscFunctionReturn(0); 202a7a1495cSBarry Smith } 203a7a1495cSBarry Smith 2044a2ae208SSatish Balay #undef __FUNCT__ 2054a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 206316643e7SJed Brown /*@ 207d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 208d763cef2SBarry Smith 209316643e7SJed Brown Collective on TS and Vec 210316643e7SJed Brown 211316643e7SJed Brown Input Parameters: 212316643e7SJed Brown + ts - the TS context 213316643e7SJed Brown . t - current time 214316643e7SJed Brown - x - state vector 215316643e7SJed Brown 216316643e7SJed Brown Output Parameter: 217316643e7SJed Brown . y - right hand side 218316643e7SJed Brown 219316643e7SJed Brown Note: 220316643e7SJed Brown Most users should not need to explicitly call this routine, as it 221316643e7SJed Brown is used internally within the nonlinear solvers. 222316643e7SJed Brown 223316643e7SJed Brown Level: developer 224316643e7SJed Brown 225316643e7SJed Brown .keywords: TS, compute 226316643e7SJed Brown 227316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 228316643e7SJed Brown @*/ 229dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 230d763cef2SBarry Smith { 231dfbe8321SBarry Smith PetscErrorCode ierr; 232d763cef2SBarry Smith 233d763cef2SBarry Smith PetscFunctionBegin; 2340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2350700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2360700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 237d763cef2SBarry Smith 2383b5f76d0SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 239d763cef2SBarry Smith 240d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 2413b5f76d0SSean Farley if (ts->userops->rhsfunction) { 242d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 2433b5f76d0SSean Farley ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 244d763cef2SBarry Smith PetscStackPop; 245214bc6a2SJed Brown } else { 246214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 247b2cd27e8SJed Brown } 24844a41b28SSean Farley 249d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 250d763cef2SBarry Smith PetscFunctionReturn(0); 251d763cef2SBarry Smith } 252d763cef2SBarry Smith 2534a2ae208SSatish Balay #undef __FUNCT__ 254214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 255214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 256214bc6a2SJed Brown { 257214bc6a2SJed Brown PetscErrorCode ierr; 258214bc6a2SJed Brown 259214bc6a2SJed Brown PetscFunctionBegin; 260214bc6a2SJed Brown if (!ts->Frhs) { 261214bc6a2SJed Brown ierr = VecDuplicate(ts->vec_sol,&ts->Frhs);CHKERRQ(ierr); 262214bc6a2SJed Brown } 263214bc6a2SJed Brown *Frhs = ts->Frhs; 264214bc6a2SJed Brown PetscFunctionReturn(0); 265214bc6a2SJed Brown } 266214bc6a2SJed Brown 267214bc6a2SJed Brown #undef __FUNCT__ 268214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 269214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 270214bc6a2SJed Brown { 271214bc6a2SJed Brown PetscErrorCode ierr; 272214bc6a2SJed Brown Mat A,B; 273214bc6a2SJed Brown 274214bc6a2SJed Brown PetscFunctionBegin; 275214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 276214bc6a2SJed Brown if (Arhs) { 277214bc6a2SJed Brown if (!ts->Arhs) { 278214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 279214bc6a2SJed Brown } 280214bc6a2SJed Brown *Arhs = ts->Arhs; 281214bc6a2SJed Brown } 282214bc6a2SJed Brown if (Brhs) { 283214bc6a2SJed Brown if (!ts->Brhs) { 284214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 285214bc6a2SJed Brown } 286214bc6a2SJed Brown *Brhs = ts->Brhs; 287214bc6a2SJed Brown } 288214bc6a2SJed Brown PetscFunctionReturn(0); 289214bc6a2SJed Brown } 290214bc6a2SJed Brown 291214bc6a2SJed Brown #undef __FUNCT__ 292316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 293316643e7SJed Brown /*@ 294316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 295316643e7SJed Brown 296316643e7SJed Brown Collective on TS and Vec 297316643e7SJed Brown 298316643e7SJed Brown Input Parameters: 299316643e7SJed Brown + ts - the TS context 300316643e7SJed Brown . t - current time 301316643e7SJed Brown . X - state vector 302214bc6a2SJed Brown . Xdot - time derivative of state vector 303214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 304316643e7SJed Brown 305316643e7SJed Brown Output Parameter: 306316643e7SJed Brown . Y - right hand side 307316643e7SJed Brown 308316643e7SJed Brown Note: 309316643e7SJed Brown Most users should not need to explicitly call this routine, as it 310316643e7SJed Brown is used internally within the nonlinear solvers. 311316643e7SJed Brown 312316643e7SJed Brown If the user did did not write their equations in implicit form, this 313316643e7SJed Brown function recasts them in implicit form. 314316643e7SJed Brown 315316643e7SJed Brown Level: developer 316316643e7SJed Brown 317316643e7SJed Brown .keywords: TS, compute 318316643e7SJed Brown 319316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 320316643e7SJed Brown @*/ 321214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 322316643e7SJed Brown { 323316643e7SJed Brown PetscErrorCode ierr; 324316643e7SJed Brown 325316643e7SJed Brown PetscFunctionBegin; 3260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3270700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3280700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3290700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 330316643e7SJed Brown 331d90be118SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 332d90be118SSean Farley 333316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 3343b5f76d0SSean Farley if (ts->userops->ifunction) { 335316643e7SJed Brown PetscStackPush("TS user implicit function"); 3363b5f76d0SSean Farley ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 337316643e7SJed Brown PetscStackPop; 338214bc6a2SJed Brown } 339214bc6a2SJed Brown if (imex) { 3403b5f76d0SSean Farley if (!ts->userops->ifunction) {ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);} 341316643e7SJed Brown } else { 3423b5f76d0SSean Farley if (!ts->userops->ifunction) { 343089b2837SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 344ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 345214bc6a2SJed Brown } else { 346214bc6a2SJed Brown Vec Frhs; 347214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 348214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 349214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 350316643e7SJed Brown } 3514a6899ffSJed Brown } 352316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 353316643e7SJed Brown PetscFunctionReturn(0); 354316643e7SJed Brown } 355316643e7SJed Brown 356316643e7SJed Brown #undef __FUNCT__ 357316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 358316643e7SJed Brown /*@ 359316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 360316643e7SJed Brown 361316643e7SJed Brown Collective on TS and Vec 362316643e7SJed Brown 363316643e7SJed Brown Input 364316643e7SJed Brown Input Parameters: 365316643e7SJed Brown + ts - the TS context 366316643e7SJed Brown . t - current timestep 367316643e7SJed Brown . X - state vector 368316643e7SJed Brown . Xdot - time derivative of state vector 369214bc6a2SJed Brown . shift - shift to apply, see note below 370214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 371316643e7SJed Brown 372316643e7SJed Brown Output Parameters: 373316643e7SJed Brown + A - Jacobian matrix 374316643e7SJed Brown . B - optional preconditioning matrix 375316643e7SJed Brown - flag - flag indicating matrix structure 376316643e7SJed Brown 377316643e7SJed Brown Notes: 378316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 379316643e7SJed Brown 380316643e7SJed Brown dF/dX + shift*dF/dXdot 381316643e7SJed Brown 382316643e7SJed Brown Most users should not need to explicitly call this routine, as it 383316643e7SJed Brown is used internally within the nonlinear solvers. 384316643e7SJed Brown 385316643e7SJed Brown Level: developer 386316643e7SJed Brown 387316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 388316643e7SJed Brown 389316643e7SJed Brown .seealso: TSSetIJacobian() 39063495f91SJed Brown @*/ 391214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 392316643e7SJed Brown { 3930026cea9SSean Farley PetscInt Xstate, Xdotstate; 394316643e7SJed Brown PetscErrorCode ierr; 395316643e7SJed Brown 396316643e7SJed Brown PetscFunctionBegin; 3970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3980700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3990700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 400316643e7SJed Brown PetscValidPointer(A,6); 4010700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 402316643e7SJed Brown PetscValidPointer(B,7); 4030700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 404316643e7SJed Brown PetscValidPointer(flg,8); 4050026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 4060026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 4070026cea9SSean Farley if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) { 4080026cea9SSean Farley *flg = ts->ijacobian.mstructure; 4090026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4100026cea9SSean Farley PetscFunctionReturn(0); 4110026cea9SSean Farley } 412316643e7SJed Brown 4133b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 414316643e7SJed Brown 4154e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 416316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 4173b5f76d0SSean Farley if (ts->userops->ijacobian) { 418316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 4193b5f76d0SSean Farley ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 420316643e7SJed Brown PetscStackPop; 421214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 422214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 423214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 4244a6899ffSJed Brown } 425214bc6a2SJed Brown if (imex) { 4263b5f76d0SSean Farley if (!ts->userops->ijacobian) { /* system was written as Xdot = F(t,X) */ 427214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 428214bc6a2SJed Brown ierr = MatShift(*A,1.0);CHKERRQ(ierr); 429214bc6a2SJed Brown if (*A != *B) { 430214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 431214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 432214bc6a2SJed Brown } 433214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 434214bc6a2SJed Brown } 435214bc6a2SJed Brown } else { 4363b5f76d0SSean Farley if (!ts->userops->ijacobian) { 437214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 438214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 439214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 440316643e7SJed Brown if (*A != *B) { 441316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 442316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 443316643e7SJed Brown } 4443b5f76d0SSean Farley } else if (ts->userops->rhsjacobian) { 445214bc6a2SJed Brown Mat Arhs,Brhs; 446214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 447214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 448214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 449214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 450214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 451214bc6a2SJed Brown if (*A != *B) { 452214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 453214bc6a2SJed Brown } 454214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 455214bc6a2SJed Brown } 456316643e7SJed Brown } 4570026cea9SSean Farley 4580026cea9SSean Farley ts->ijacobian.time = t; 4590026cea9SSean Farley ts->ijacobian.X = X; 4600026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 4610026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 4620026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 4630026cea9SSean Farley ts->ijacobian.shift = shift; 4640026cea9SSean Farley ts->ijacobian.imex = imex; 4650026cea9SSean Farley ts->ijacobian.mstructure = *flg; 466316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 467316643e7SJed Brown PetscFunctionReturn(0); 468316643e7SJed Brown } 469316643e7SJed Brown 470316643e7SJed Brown #undef __FUNCT__ 4714a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 472d763cef2SBarry Smith /*@C 473d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 474d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 475d763cef2SBarry Smith 4763f9fe445SBarry Smith Logically Collective on TS 477d763cef2SBarry Smith 478d763cef2SBarry Smith Input Parameters: 479d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 480ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 481d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 482d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 483d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 484d763cef2SBarry Smith 485d763cef2SBarry Smith Calling sequence of func: 48687828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 487d763cef2SBarry Smith 488d763cef2SBarry Smith + t - current timestep 489d763cef2SBarry Smith . u - input vector 490d763cef2SBarry Smith . F - function vector 491d763cef2SBarry Smith - ctx - [optional] user-defined function context 492d763cef2SBarry Smith 493d763cef2SBarry Smith Important: 49476f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 495d763cef2SBarry Smith 496d763cef2SBarry Smith Level: beginner 497d763cef2SBarry Smith 498d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 499d763cef2SBarry Smith 50076f2fa84SHong Zhang .seealso: TSSetMatrices() 501d763cef2SBarry Smith @*/ 502089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 503d763cef2SBarry Smith { 504089b2837SJed Brown PetscErrorCode ierr; 505089b2837SJed Brown SNES snes; 506d763cef2SBarry Smith 507089b2837SJed Brown PetscFunctionBegin; 5080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 509ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 5103b5f76d0SSean Farley if (f) ts->userops->rhsfunction = f; 5114e684422SJed Brown if (ctx) ts->funP = ctx; 512089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 513089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 514d763cef2SBarry Smith PetscFunctionReturn(0); 515d763cef2SBarry Smith } 516d763cef2SBarry Smith 5174a2ae208SSatish Balay #undef __FUNCT__ 5184a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 519d763cef2SBarry Smith /*@C 520d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 521d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 52276f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 523d763cef2SBarry Smith 5243f9fe445SBarry Smith Logically Collective on TS 525d763cef2SBarry Smith 526d763cef2SBarry Smith Input Parameters: 527d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 528d763cef2SBarry Smith . A - Jacobian matrix 529d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 530d763cef2SBarry Smith . f - the Jacobian evaluation routine 531d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 532d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 533d763cef2SBarry Smith 534d763cef2SBarry Smith Calling sequence of func: 53587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 536d763cef2SBarry Smith 537d763cef2SBarry Smith + t - current timestep 538d763cef2SBarry Smith . u - input vector 539d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 540d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 541d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 54294b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 543d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 544d763cef2SBarry Smith 545d763cef2SBarry Smith Notes: 54694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 547d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 548d763cef2SBarry Smith 549d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 550d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 55156335db2SHong Zhang completely new matrix structure (not just different matrix elements) 552d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 553d763cef2SBarry Smith throughout the global iterations. 554d763cef2SBarry Smith 555d763cef2SBarry Smith Level: beginner 556d763cef2SBarry Smith 557d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 558d763cef2SBarry Smith 559d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 56076f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 561d763cef2SBarry Smith 562d763cef2SBarry Smith @*/ 563089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 564d763cef2SBarry Smith { 565277b19d0SLisandro Dalcin PetscErrorCode ierr; 566089b2837SJed Brown SNES snes; 567277b19d0SLisandro Dalcin 568d763cef2SBarry Smith PetscFunctionBegin; 5690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 570277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 571277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 572277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 573277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 574d763cef2SBarry Smith 5753b5f76d0SSean Farley if (f) ts->userops->rhsjacobian = f; 576277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 577089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 578dabe61deSJed Brown if (!ts->userops->ijacobian) { 579089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 5800e4ef248SJed Brown } 5810e4ef248SJed Brown if (A) { 5820e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 5830e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 5840e4ef248SJed Brown ts->Arhs = A; 5850e4ef248SJed Brown } 5860e4ef248SJed Brown if (B) { 5870e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 5880e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 5890e4ef248SJed Brown ts->Brhs = B; 5900e4ef248SJed Brown } 591d763cef2SBarry Smith PetscFunctionReturn(0); 592d763cef2SBarry Smith } 593d763cef2SBarry Smith 594316643e7SJed Brown 595316643e7SJed Brown #undef __FUNCT__ 596316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 597316643e7SJed Brown /*@C 598316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 599316643e7SJed Brown 6003f9fe445SBarry Smith Logically Collective on TS 601316643e7SJed Brown 602316643e7SJed Brown Input Parameters: 603316643e7SJed Brown + ts - the TS context obtained from TSCreate() 604ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 605316643e7SJed Brown . f - the function evaluation routine 606316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 607316643e7SJed Brown 608316643e7SJed Brown Calling sequence of f: 609316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 610316643e7SJed Brown 611316643e7SJed Brown + t - time at step/stage being solved 612316643e7SJed Brown . u - state vector 613316643e7SJed Brown . u_t - time derivative of state vector 614316643e7SJed Brown . F - function vector 615316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 616316643e7SJed Brown 617316643e7SJed Brown Important: 618316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 619316643e7SJed Brown 620316643e7SJed Brown Level: beginner 621316643e7SJed Brown 622316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 623316643e7SJed Brown 624316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 625316643e7SJed Brown @*/ 626089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 627316643e7SJed Brown { 628089b2837SJed Brown PetscErrorCode ierr; 629089b2837SJed Brown SNES snes; 630316643e7SJed Brown 631316643e7SJed Brown PetscFunctionBegin; 6320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 633ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 6343b5f76d0SSean Farley if (f) ts->userops->ifunction = f; 635089b2837SJed Brown if (ctx) ts->funP = ctx; 636089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 637089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 638089b2837SJed Brown PetscFunctionReturn(0); 639089b2837SJed Brown } 640089b2837SJed Brown 641089b2837SJed Brown #undef __FUNCT__ 642089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 643089b2837SJed Brown /*@C 644089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 645089b2837SJed Brown 646089b2837SJed Brown Not Collective 647089b2837SJed Brown 648089b2837SJed Brown Input Parameter: 649089b2837SJed Brown . ts - the TS context 650089b2837SJed Brown 651089b2837SJed Brown Output Parameter: 652089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 653089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 654089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 655089b2837SJed Brown 656089b2837SJed Brown Level: advanced 657089b2837SJed Brown 658089b2837SJed Brown .keywords: TS, nonlinear, get, function 659089b2837SJed Brown 660089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 661089b2837SJed Brown @*/ 662089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 663089b2837SJed Brown { 664089b2837SJed Brown PetscErrorCode ierr; 665089b2837SJed Brown SNES snes; 666089b2837SJed Brown 667089b2837SJed Brown PetscFunctionBegin; 668089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 669089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 670089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 6713b5f76d0SSean Farley if (func) *func = ts->userops->ifunction; 672089b2837SJed Brown if (ctx) *ctx = ts->funP; 673089b2837SJed Brown PetscFunctionReturn(0); 674089b2837SJed Brown } 675089b2837SJed Brown 676089b2837SJed Brown #undef __FUNCT__ 677089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 678089b2837SJed Brown /*@C 679089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 680089b2837SJed Brown 681089b2837SJed Brown Not Collective 682089b2837SJed Brown 683089b2837SJed Brown Input Parameter: 684089b2837SJed Brown . ts - the TS context 685089b2837SJed Brown 686089b2837SJed Brown Output Parameter: 687089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 688089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 689089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 690089b2837SJed Brown 691089b2837SJed Brown Level: advanced 692089b2837SJed Brown 693089b2837SJed Brown .keywords: TS, nonlinear, get, function 694089b2837SJed Brown 695089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 696089b2837SJed Brown @*/ 697089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 698089b2837SJed Brown { 699089b2837SJed Brown PetscErrorCode ierr; 700089b2837SJed Brown SNES snes; 701089b2837SJed Brown 702089b2837SJed Brown PetscFunctionBegin; 703089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 704089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 705089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7063b5f76d0SSean Farley if (func) *func = ts->userops->rhsfunction; 707089b2837SJed Brown if (ctx) *ctx = ts->funP; 708316643e7SJed Brown PetscFunctionReturn(0); 709316643e7SJed Brown } 710316643e7SJed Brown 711316643e7SJed Brown #undef __FUNCT__ 712316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 713316643e7SJed Brown /*@C 714a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 715a4f0a591SBarry Smith you provided with TSSetIFunction(). 716316643e7SJed Brown 7173f9fe445SBarry Smith Logically Collective on TS 718316643e7SJed Brown 719316643e7SJed Brown Input Parameters: 720316643e7SJed Brown + ts - the TS context obtained from TSCreate() 721316643e7SJed Brown . A - Jacobian matrix 722316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 723316643e7SJed Brown . f - the Jacobian evaluation routine 724316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 725316643e7SJed Brown 726316643e7SJed Brown Calling sequence of f: 7271b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 728316643e7SJed Brown 729316643e7SJed Brown + t - time at step/stage being solved 7301b4a444bSJed Brown . U - state vector 7311b4a444bSJed Brown . U_t - time derivative of state vector 732316643e7SJed Brown . a - shift 7331b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 734316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 735316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 736316643e7SJed Brown structure (same as flag in KSPSetOperators()) 737316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 738316643e7SJed Brown 739316643e7SJed Brown Notes: 740316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 741316643e7SJed Brown 742a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 743a4f0a591SBarry 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. 744a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 745a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 746a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 747a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 748a4f0a591SBarry Smith 749316643e7SJed Brown Level: beginner 750316643e7SJed Brown 751316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 752316643e7SJed Brown 753316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 754316643e7SJed Brown 755316643e7SJed Brown @*/ 7567087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 757316643e7SJed Brown { 758316643e7SJed Brown PetscErrorCode ierr; 759089b2837SJed Brown SNES snes; 760316643e7SJed Brown 761316643e7SJed Brown PetscFunctionBegin; 7620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7630700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 7640700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 765316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 766316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 7673b5f76d0SSean Farley if (f) ts->userops->ijacobian = f; 768316643e7SJed Brown if (ctx) ts->jacP = ctx; 769089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 770089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 771316643e7SJed Brown PetscFunctionReturn(0); 772316643e7SJed Brown } 773316643e7SJed Brown 7744a2ae208SSatish Balay #undef __FUNCT__ 7754a2ae208SSatish Balay #define __FUNCT__ "TSView" 7767e2c5f70SBarry Smith /*@C 777d763cef2SBarry Smith TSView - Prints the TS data structure. 778d763cef2SBarry Smith 7794c49b128SBarry Smith Collective on TS 780d763cef2SBarry Smith 781d763cef2SBarry Smith Input Parameters: 782d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 783d763cef2SBarry Smith - viewer - visualization context 784d763cef2SBarry Smith 785d763cef2SBarry Smith Options Database Key: 786d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 787d763cef2SBarry Smith 788d763cef2SBarry Smith Notes: 789d763cef2SBarry Smith The available visualization contexts include 790b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 791b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 792d763cef2SBarry Smith output where only the first processor opens 793d763cef2SBarry Smith the file. All other processors send their 794d763cef2SBarry Smith data to the first processor to print. 795d763cef2SBarry Smith 796d763cef2SBarry Smith The user can open an alternative visualization context with 797b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 798d763cef2SBarry Smith 799d763cef2SBarry Smith Level: beginner 800d763cef2SBarry Smith 801d763cef2SBarry Smith .keywords: TS, timestep, view 802d763cef2SBarry Smith 803b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 804d763cef2SBarry Smith @*/ 8057087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 806d763cef2SBarry Smith { 807dfbe8321SBarry Smith PetscErrorCode ierr; 808a313700dSBarry Smith const TSType type; 809089b2837SJed Brown PetscBool iascii,isstring,isundials; 810d763cef2SBarry Smith 811d763cef2SBarry Smith PetscFunctionBegin; 8120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8133050cee2SBarry Smith if (!viewer) { 8147adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8153050cee2SBarry Smith } 8160700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 817c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 818fd16b177SBarry Smith 8192692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8202692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 82132077d6dSBarry Smith if (iascii) { 822317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 823000e7ae3SMatthew Knepley if (ts->ops->view) { 824b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 825000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 826b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 827d763cef2SBarry Smith } 82877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 829a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 830d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 83177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 832193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr); 833d763cef2SBarry Smith } 83477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 835193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 8360f5bd95cSBarry Smith } else if (isstring) { 837a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 838b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 839d763cef2SBarry Smith } 840b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 841089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 842089b2837SJed Brown if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 843b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 844d763cef2SBarry Smith PetscFunctionReturn(0); 845d763cef2SBarry Smith } 846d763cef2SBarry Smith 847d763cef2SBarry Smith 8484a2ae208SSatish Balay #undef __FUNCT__ 8494a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 850b07ff414SBarry Smith /*@ 851d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 852d763cef2SBarry Smith the timesteppers. 853d763cef2SBarry Smith 8543f9fe445SBarry Smith Logically Collective on TS 855d763cef2SBarry Smith 856d763cef2SBarry Smith Input Parameters: 857d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 858d763cef2SBarry Smith - usrP - optional user context 859d763cef2SBarry Smith 860d763cef2SBarry Smith Level: intermediate 861d763cef2SBarry Smith 862d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 863d763cef2SBarry Smith 864d763cef2SBarry Smith .seealso: TSGetApplicationContext() 865d763cef2SBarry Smith @*/ 8667087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 867d763cef2SBarry Smith { 868d763cef2SBarry Smith PetscFunctionBegin; 8690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 870d763cef2SBarry Smith ts->user = usrP; 871d763cef2SBarry Smith PetscFunctionReturn(0); 872d763cef2SBarry Smith } 873d763cef2SBarry Smith 8744a2ae208SSatish Balay #undef __FUNCT__ 8754a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 876b07ff414SBarry Smith /*@ 877d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 878d763cef2SBarry Smith timestepper. 879d763cef2SBarry Smith 880d763cef2SBarry Smith Not Collective 881d763cef2SBarry Smith 882d763cef2SBarry Smith Input Parameter: 883d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 884d763cef2SBarry Smith 885d763cef2SBarry Smith Output Parameter: 886d763cef2SBarry Smith . usrP - user context 887d763cef2SBarry Smith 888d763cef2SBarry Smith Level: intermediate 889d763cef2SBarry Smith 890d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 891d763cef2SBarry Smith 892d763cef2SBarry Smith .seealso: TSSetApplicationContext() 893d763cef2SBarry Smith @*/ 894e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 895d763cef2SBarry Smith { 896d763cef2SBarry Smith PetscFunctionBegin; 8970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 898e71120c6SJed Brown *(void**)usrP = ts->user; 899d763cef2SBarry Smith PetscFunctionReturn(0); 900d763cef2SBarry Smith } 901d763cef2SBarry Smith 9024a2ae208SSatish Balay #undef __FUNCT__ 9034a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 904d763cef2SBarry Smith /*@ 905d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 906d763cef2SBarry Smith 907d763cef2SBarry Smith Not Collective 908d763cef2SBarry Smith 909d763cef2SBarry Smith Input Parameter: 910d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 911d763cef2SBarry Smith 912d763cef2SBarry Smith Output Parameter: 913d763cef2SBarry Smith . iter - number steps so far 914d763cef2SBarry Smith 915d763cef2SBarry Smith Level: intermediate 916d763cef2SBarry Smith 917d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 918d763cef2SBarry Smith @*/ 9197087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 920d763cef2SBarry Smith { 921d763cef2SBarry Smith PetscFunctionBegin; 9220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9234482741eSBarry Smith PetscValidIntPointer(iter,2); 924d763cef2SBarry Smith *iter = ts->steps; 925d763cef2SBarry Smith PetscFunctionReturn(0); 926d763cef2SBarry Smith } 927d763cef2SBarry Smith 9284a2ae208SSatish Balay #undef __FUNCT__ 9294a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 930d763cef2SBarry Smith /*@ 931d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 932d763cef2SBarry Smith as well as the initial time. 933d763cef2SBarry Smith 9343f9fe445SBarry Smith Logically Collective on TS 935d763cef2SBarry Smith 936d763cef2SBarry Smith Input Parameters: 937d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 938d763cef2SBarry Smith . initial_time - the initial time 939d763cef2SBarry Smith - time_step - the size of the timestep 940d763cef2SBarry Smith 941d763cef2SBarry Smith Level: intermediate 942d763cef2SBarry Smith 943d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 944d763cef2SBarry Smith 945d763cef2SBarry Smith .keywords: TS, set, initial, timestep 946d763cef2SBarry Smith @*/ 9477087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 948d763cef2SBarry Smith { 949e144a568SJed Brown PetscErrorCode ierr; 950e144a568SJed Brown 951d763cef2SBarry Smith PetscFunctionBegin; 9520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 953e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 954d763cef2SBarry Smith ts->initial_time_step = time_step; 955d763cef2SBarry Smith ts->ptime = initial_time; 956d763cef2SBarry Smith PetscFunctionReturn(0); 957d763cef2SBarry Smith } 958d763cef2SBarry Smith 9594a2ae208SSatish Balay #undef __FUNCT__ 9604a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 961d763cef2SBarry Smith /*@ 962d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 963d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 964d763cef2SBarry Smith 9653f9fe445SBarry Smith Logically Collective on TS 966d763cef2SBarry Smith 967d763cef2SBarry Smith Input Parameters: 968d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 969d763cef2SBarry Smith - time_step - the size of the timestep 970d763cef2SBarry Smith 971d763cef2SBarry Smith Level: intermediate 972d763cef2SBarry Smith 973d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 974d763cef2SBarry Smith 975d763cef2SBarry Smith .keywords: TS, set, timestep 976d763cef2SBarry Smith @*/ 9777087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 978d763cef2SBarry Smith { 979d763cef2SBarry Smith PetscFunctionBegin; 9800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 981c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 982d763cef2SBarry Smith ts->time_step = time_step; 983193ac0bcSJed Brown ts->next_time_step = time_step; 984d763cef2SBarry Smith PetscFunctionReturn(0); 985d763cef2SBarry Smith } 986d763cef2SBarry Smith 9874a2ae208SSatish Balay #undef __FUNCT__ 9884a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 989d763cef2SBarry Smith /*@ 990d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 991d763cef2SBarry Smith 992d763cef2SBarry Smith Not Collective 993d763cef2SBarry Smith 994d763cef2SBarry Smith Input Parameter: 995d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 996d763cef2SBarry Smith 997d763cef2SBarry Smith Output Parameter: 998d763cef2SBarry Smith . dt - the current timestep size 999d763cef2SBarry Smith 1000d763cef2SBarry Smith Level: intermediate 1001d763cef2SBarry Smith 1002d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1003d763cef2SBarry Smith 1004d763cef2SBarry Smith .keywords: TS, get, timestep 1005d763cef2SBarry Smith @*/ 10067087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1007d763cef2SBarry Smith { 1008d763cef2SBarry Smith PetscFunctionBegin; 10090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10104482741eSBarry Smith PetscValidDoublePointer(dt,2); 1011d763cef2SBarry Smith *dt = ts->time_step; 1012d763cef2SBarry Smith PetscFunctionReturn(0); 1013d763cef2SBarry Smith } 1014d763cef2SBarry Smith 10154a2ae208SSatish Balay #undef __FUNCT__ 10164a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1017d8e5e3e6SSatish Balay /*@ 1018d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1019d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1020d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1021d763cef2SBarry Smith the solution at the next timestep has been calculated. 1022d763cef2SBarry Smith 1023d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1024d763cef2SBarry Smith 1025d763cef2SBarry Smith Input Parameter: 1026d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1027d763cef2SBarry Smith 1028d763cef2SBarry Smith Output Parameter: 1029d763cef2SBarry Smith . v - the vector containing the solution 1030d763cef2SBarry Smith 1031d763cef2SBarry Smith Level: intermediate 1032d763cef2SBarry Smith 1033d763cef2SBarry Smith .seealso: TSGetTimeStep() 1034d763cef2SBarry Smith 1035d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1036d763cef2SBarry Smith @*/ 10377087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1038d763cef2SBarry Smith { 1039d763cef2SBarry Smith PetscFunctionBegin; 10400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10414482741eSBarry Smith PetscValidPointer(v,2); 10428737fe31SLisandro Dalcin *v = ts->vec_sol; 1043d763cef2SBarry Smith PetscFunctionReturn(0); 1044d763cef2SBarry Smith } 1045d763cef2SBarry Smith 1046bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 10474a2ae208SSatish Balay #undef __FUNCT__ 1048bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1049d8e5e3e6SSatish Balay /*@ 1050bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1051d763cef2SBarry Smith 1052bdad233fSMatthew Knepley Not collective 1053d763cef2SBarry Smith 1054bdad233fSMatthew Knepley Input Parameters: 1055bdad233fSMatthew Knepley + ts - The TS 1056bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1057d763cef2SBarry Smith .vb 1058d763cef2SBarry Smith U_t = A U 1059d763cef2SBarry Smith U_t = A(t) U 1060d763cef2SBarry Smith U_t = F(t,U) 1061d763cef2SBarry Smith .ve 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith Level: beginner 1064d763cef2SBarry Smith 1065bdad233fSMatthew Knepley .keywords: TS, problem type 1066bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1067d763cef2SBarry Smith @*/ 10687087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1069a7cc72afSBarry Smith { 10709e2a6581SJed Brown PetscErrorCode ierr; 10719e2a6581SJed Brown 1072d763cef2SBarry Smith PetscFunctionBegin; 10730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1074bdad233fSMatthew Knepley ts->problem_type = type; 10759e2a6581SJed Brown if (type == TS_LINEAR) { 10769e2a6581SJed Brown SNES snes; 10779e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10789e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 10799e2a6581SJed Brown } 1080d763cef2SBarry Smith PetscFunctionReturn(0); 1081d763cef2SBarry Smith } 1082d763cef2SBarry Smith 1083bdad233fSMatthew Knepley #undef __FUNCT__ 1084bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1085bdad233fSMatthew Knepley /*@C 1086bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1087bdad233fSMatthew Knepley 1088bdad233fSMatthew Knepley Not collective 1089bdad233fSMatthew Knepley 1090bdad233fSMatthew Knepley Input Parameter: 1091bdad233fSMatthew Knepley . ts - The TS 1092bdad233fSMatthew Knepley 1093bdad233fSMatthew Knepley Output Parameter: 1094bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1095bdad233fSMatthew Knepley .vb 1096089b2837SJed Brown M U_t = A U 1097089b2837SJed Brown M(t) U_t = A(t) U 1098bdad233fSMatthew Knepley U_t = F(t,U) 1099bdad233fSMatthew Knepley .ve 1100bdad233fSMatthew Knepley 1101bdad233fSMatthew Knepley Level: beginner 1102bdad233fSMatthew Knepley 1103bdad233fSMatthew Knepley .keywords: TS, problem type 1104bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1105bdad233fSMatthew Knepley @*/ 11067087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1107a7cc72afSBarry Smith { 1108bdad233fSMatthew Knepley PetscFunctionBegin; 11090700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11104482741eSBarry Smith PetscValidIntPointer(type,2); 1111bdad233fSMatthew Knepley *type = ts->problem_type; 1112bdad233fSMatthew Knepley PetscFunctionReturn(0); 1113bdad233fSMatthew Knepley } 1114d763cef2SBarry Smith 11154a2ae208SSatish Balay #undef __FUNCT__ 11164a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1117d763cef2SBarry Smith /*@ 1118d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1119d763cef2SBarry Smith of a timestepper. 1120d763cef2SBarry Smith 1121d763cef2SBarry Smith Collective on TS 1122d763cef2SBarry Smith 1123d763cef2SBarry Smith Input Parameter: 1124d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1125d763cef2SBarry Smith 1126d763cef2SBarry Smith Notes: 1127d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1128d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1129d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1130d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1131d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1132d763cef2SBarry Smith 1133d763cef2SBarry Smith Level: advanced 1134d763cef2SBarry Smith 1135d763cef2SBarry Smith .keywords: TS, timestep, setup 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1138d763cef2SBarry Smith @*/ 11397087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1140d763cef2SBarry Smith { 1141dfbe8321SBarry Smith PetscErrorCode ierr; 1142d763cef2SBarry Smith 1143d763cef2SBarry Smith PetscFunctionBegin; 11440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1145277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1146277b19d0SLisandro Dalcin 11477adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 11489596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1149d763cef2SBarry Smith } 1150277b19d0SLisandro Dalcin 1151277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1152277b19d0SLisandro Dalcin 1153277b19d0SLisandro Dalcin if (ts->ops->setup) { 1154000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1155277b19d0SLisandro Dalcin } 1156277b19d0SLisandro Dalcin 1157277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1158277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1159277b19d0SLisandro Dalcin } 1160277b19d0SLisandro Dalcin 1161277b19d0SLisandro Dalcin #undef __FUNCT__ 1162277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1163277b19d0SLisandro Dalcin /*@ 1164277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1165277b19d0SLisandro Dalcin 1166277b19d0SLisandro Dalcin Collective on TS 1167277b19d0SLisandro Dalcin 1168277b19d0SLisandro Dalcin Input Parameter: 1169277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1170277b19d0SLisandro Dalcin 1171277b19d0SLisandro Dalcin Level: beginner 1172277b19d0SLisandro Dalcin 1173277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1174277b19d0SLisandro Dalcin 1175277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1176277b19d0SLisandro Dalcin @*/ 1177277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1178277b19d0SLisandro Dalcin { 1179277b19d0SLisandro Dalcin PetscErrorCode ierr; 1180277b19d0SLisandro Dalcin 1181277b19d0SLisandro Dalcin PetscFunctionBegin; 1182277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1183277b19d0SLisandro Dalcin if (ts->ops->reset) { 1184277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1185277b19d0SLisandro Dalcin } 1186277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 11874e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 11884e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1189214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 11906bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1191277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1192277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1193d763cef2SBarry Smith PetscFunctionReturn(0); 1194d763cef2SBarry Smith } 1195d763cef2SBarry Smith 11964a2ae208SSatish Balay #undef __FUNCT__ 11974a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1198d8e5e3e6SSatish Balay /*@ 1199d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1200d763cef2SBarry Smith with TSCreate(). 1201d763cef2SBarry Smith 1202d763cef2SBarry Smith Collective on TS 1203d763cef2SBarry Smith 1204d763cef2SBarry Smith Input Parameter: 1205d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1206d763cef2SBarry Smith 1207d763cef2SBarry Smith Level: beginner 1208d763cef2SBarry Smith 1209d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1210d763cef2SBarry Smith 1211d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1212d763cef2SBarry Smith @*/ 12136bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1214d763cef2SBarry Smith { 12156849ba73SBarry Smith PetscErrorCode ierr; 1216d763cef2SBarry Smith 1217d763cef2SBarry Smith PetscFunctionBegin; 12186bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12196bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12206bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1221d763cef2SBarry Smith 12226bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1223277b19d0SLisandro Dalcin 1224be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12256bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 12266bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 12276d4c513bSLisandro Dalcin 12286bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 12296bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 12306bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 12316d4c513bSLisandro Dalcin 12323b5f76d0SSean Farley ierr = PetscFree((*ts)->userops); 12333b5f76d0SSean Farley 1234a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1235d763cef2SBarry Smith PetscFunctionReturn(0); 1236d763cef2SBarry Smith } 1237d763cef2SBarry Smith 12384a2ae208SSatish Balay #undef __FUNCT__ 12394a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1240d8e5e3e6SSatish Balay /*@ 1241d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1242d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1243d763cef2SBarry Smith 1244d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1245d763cef2SBarry Smith 1246d763cef2SBarry Smith Input Parameter: 1247d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1248d763cef2SBarry Smith 1249d763cef2SBarry Smith Output Parameter: 1250d763cef2SBarry Smith . snes - the nonlinear solver context 1251d763cef2SBarry Smith 1252d763cef2SBarry Smith Notes: 1253d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1254d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 125594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1256d763cef2SBarry Smith 1257d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1258d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1259d763cef2SBarry Smith 1260d763cef2SBarry Smith Level: beginner 1261d763cef2SBarry Smith 1262d763cef2SBarry Smith .keywords: timestep, get, SNES 1263d763cef2SBarry Smith @*/ 12647087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1265d763cef2SBarry Smith { 1266d372ba47SLisandro Dalcin PetscErrorCode ierr; 1267d372ba47SLisandro Dalcin 1268d763cef2SBarry Smith PetscFunctionBegin; 12690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12704482741eSBarry Smith PetscValidPointer(snes,2); 1271d372ba47SLisandro Dalcin if (!ts->snes) { 1272d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1273d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1274d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 12759e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 12769e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 12779e2a6581SJed Brown } 1278d372ba47SLisandro Dalcin } 1279d763cef2SBarry Smith *snes = ts->snes; 1280d763cef2SBarry Smith PetscFunctionReturn(0); 1281d763cef2SBarry Smith } 1282d763cef2SBarry Smith 12834a2ae208SSatish Balay #undef __FUNCT__ 128494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1285d8e5e3e6SSatish Balay /*@ 128694b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1287d763cef2SBarry Smith a TS (timestepper) context. 1288d763cef2SBarry Smith 128994b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1290d763cef2SBarry Smith 1291d763cef2SBarry Smith Input Parameter: 1292d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1293d763cef2SBarry Smith 1294d763cef2SBarry Smith Output Parameter: 129594b7f48cSBarry Smith . ksp - the nonlinear solver context 1296d763cef2SBarry Smith 1297d763cef2SBarry Smith Notes: 129894b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1299d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1300d763cef2SBarry Smith KSP and PC contexts as well. 1301d763cef2SBarry Smith 130294b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 130394b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1304d763cef2SBarry Smith 1305d763cef2SBarry Smith Level: beginner 1306d763cef2SBarry Smith 130794b7f48cSBarry Smith .keywords: timestep, get, KSP 1308d763cef2SBarry Smith @*/ 13097087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1310d763cef2SBarry Smith { 1311d372ba47SLisandro Dalcin PetscErrorCode ierr; 1312089b2837SJed Brown SNES snes; 1313d372ba47SLisandro Dalcin 1314d763cef2SBarry Smith PetscFunctionBegin; 13150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13164482741eSBarry Smith PetscValidPointer(ksp,2); 131717186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1318e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1319089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1320089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1321d763cef2SBarry Smith PetscFunctionReturn(0); 1322d763cef2SBarry Smith } 1323d763cef2SBarry Smith 1324d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1325d763cef2SBarry Smith 13264a2ae208SSatish Balay #undef __FUNCT__ 1327adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1328adb62b0dSMatthew Knepley /*@ 1329adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1330adb62b0dSMatthew Knepley maximum time for iteration. 1331adb62b0dSMatthew Knepley 13323f9fe445SBarry Smith Not Collective 1333adb62b0dSMatthew Knepley 1334adb62b0dSMatthew Knepley Input Parameters: 1335adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1336adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1337adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1338adb62b0dSMatthew Knepley 1339adb62b0dSMatthew Knepley Level: intermediate 1340adb62b0dSMatthew Knepley 1341adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1342adb62b0dSMatthew Knepley @*/ 13437087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1344adb62b0dSMatthew Knepley { 1345adb62b0dSMatthew Knepley PetscFunctionBegin; 13460700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1347abc0a331SBarry Smith if (maxsteps) { 13484482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1349adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1350adb62b0dSMatthew Knepley } 1351abc0a331SBarry Smith if (maxtime ) { 13524482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1353adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1354adb62b0dSMatthew Knepley } 1355adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1356adb62b0dSMatthew Knepley } 1357adb62b0dSMatthew Knepley 1358adb62b0dSMatthew Knepley #undef __FUNCT__ 13594a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1360d763cef2SBarry Smith /*@ 1361d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1362d763cef2SBarry Smith maximum time for iteration. 1363d763cef2SBarry Smith 13643f9fe445SBarry Smith Logically Collective on TS 1365d763cef2SBarry Smith 1366d763cef2SBarry Smith Input Parameters: 1367d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1368d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1369d763cef2SBarry Smith - maxtime - final time to iterate to 1370d763cef2SBarry Smith 1371d763cef2SBarry Smith Options Database Keys: 1372d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1373d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1374d763cef2SBarry Smith 1375d763cef2SBarry Smith Notes: 1376d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1377d763cef2SBarry Smith 1378d763cef2SBarry Smith Level: intermediate 1379d763cef2SBarry Smith 1380d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1381d763cef2SBarry Smith @*/ 13827087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1383d763cef2SBarry Smith { 1384d763cef2SBarry Smith PetscFunctionBegin; 13850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1386c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1387c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 138839b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 138939b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1390d763cef2SBarry Smith PetscFunctionReturn(0); 1391d763cef2SBarry Smith } 1392d763cef2SBarry Smith 13934a2ae208SSatish Balay #undef __FUNCT__ 13944a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1395d763cef2SBarry Smith /*@ 1396d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1397d763cef2SBarry Smith for use by the TS routines. 1398d763cef2SBarry Smith 13993f9fe445SBarry Smith Logically Collective on TS and Vec 1400d763cef2SBarry Smith 1401d763cef2SBarry Smith Input Parameters: 1402d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1403d763cef2SBarry Smith - x - the solution vector 1404d763cef2SBarry Smith 1405d763cef2SBarry Smith Level: beginner 1406d763cef2SBarry Smith 1407d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1408d763cef2SBarry Smith @*/ 14097087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1410d763cef2SBarry Smith { 14118737fe31SLisandro Dalcin PetscErrorCode ierr; 14128737fe31SLisandro Dalcin 1413d763cef2SBarry Smith PetscFunctionBegin; 14140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14150700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14168737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14176bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14188737fe31SLisandro Dalcin ts->vec_sol = x; 1419d763cef2SBarry Smith PetscFunctionReturn(0); 1420d763cef2SBarry Smith } 1421d763cef2SBarry Smith 1422e74ef692SMatthew Knepley #undef __FUNCT__ 1423e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1424ac226902SBarry Smith /*@C 1425000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14263f2090d5SJed Brown called once at the beginning of each time step. 1427000e7ae3SMatthew Knepley 14283f9fe445SBarry Smith Logically Collective on TS 1429000e7ae3SMatthew Knepley 1430000e7ae3SMatthew Knepley Input Parameters: 1431000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1432000e7ae3SMatthew Knepley - func - The function 1433000e7ae3SMatthew Knepley 1434000e7ae3SMatthew Knepley Calling sequence of func: 1435000e7ae3SMatthew Knepley . func (TS ts); 1436000e7ae3SMatthew Knepley 1437000e7ae3SMatthew Knepley Level: intermediate 1438000e7ae3SMatthew Knepley 1439000e7ae3SMatthew Knepley .keywords: TS, timestep 1440000e7ae3SMatthew Knepley @*/ 14417087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1442000e7ae3SMatthew Knepley { 1443000e7ae3SMatthew Knepley PetscFunctionBegin; 14440700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1445000e7ae3SMatthew Knepley ts->ops->prestep = func; 1446000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1447000e7ae3SMatthew Knepley } 1448000e7ae3SMatthew Knepley 1449e74ef692SMatthew Knepley #undef __FUNCT__ 14503f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 14513f2090d5SJed Brown /*@C 14523f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 14533f2090d5SJed Brown 14543f2090d5SJed Brown Collective on TS 14553f2090d5SJed Brown 14563f2090d5SJed Brown Input Parameters: 14573f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 14583f2090d5SJed Brown 14593f2090d5SJed Brown Notes: 14603f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 14613f2090d5SJed Brown so most users would not generally call this routine themselves. 14623f2090d5SJed Brown 14633f2090d5SJed Brown Level: developer 14643f2090d5SJed Brown 14653f2090d5SJed Brown .keywords: TS, timestep 14663f2090d5SJed Brown @*/ 14677087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 14683f2090d5SJed Brown { 14693f2090d5SJed Brown PetscErrorCode ierr; 14703f2090d5SJed Brown 14713f2090d5SJed Brown PetscFunctionBegin; 14720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 147372ac3e02SJed Brown if (ts->ops->prestep) { 14743f2090d5SJed Brown PetscStackPush("TS PreStep function"); 14753f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 14763f2090d5SJed Brown PetscStackPop; 1477312ce896SJed Brown } 14783f2090d5SJed Brown PetscFunctionReturn(0); 14793f2090d5SJed Brown } 14803f2090d5SJed Brown 14813f2090d5SJed Brown #undef __FUNCT__ 1482e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1483ac226902SBarry Smith /*@C 1484000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 14853f2090d5SJed Brown called once at the end of each time step. 1486000e7ae3SMatthew Knepley 14873f9fe445SBarry Smith Logically Collective on TS 1488000e7ae3SMatthew Knepley 1489000e7ae3SMatthew Knepley Input Parameters: 1490000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1491000e7ae3SMatthew Knepley - func - The function 1492000e7ae3SMatthew Knepley 1493000e7ae3SMatthew Knepley Calling sequence of func: 1494000e7ae3SMatthew Knepley . func (TS ts); 1495000e7ae3SMatthew Knepley 1496000e7ae3SMatthew Knepley Level: intermediate 1497000e7ae3SMatthew Knepley 1498000e7ae3SMatthew Knepley .keywords: TS, timestep 1499000e7ae3SMatthew Knepley @*/ 15007087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1501000e7ae3SMatthew Knepley { 1502000e7ae3SMatthew Knepley PetscFunctionBegin; 15030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1504000e7ae3SMatthew Knepley ts->ops->poststep = func; 1505000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1506000e7ae3SMatthew Knepley } 1507000e7ae3SMatthew Knepley 1508e74ef692SMatthew Knepley #undef __FUNCT__ 15093f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15103f2090d5SJed Brown /*@C 15113f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15123f2090d5SJed Brown 15133f2090d5SJed Brown Collective on TS 15143f2090d5SJed Brown 15153f2090d5SJed Brown Input Parameters: 15163f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15173f2090d5SJed Brown 15183f2090d5SJed Brown Notes: 15193f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15203f2090d5SJed Brown so most users would not generally call this routine themselves. 15213f2090d5SJed Brown 15223f2090d5SJed Brown Level: developer 15233f2090d5SJed Brown 15243f2090d5SJed Brown .keywords: TS, timestep 15253f2090d5SJed Brown @*/ 15267087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15273f2090d5SJed Brown { 15283f2090d5SJed Brown PetscErrorCode ierr; 15293f2090d5SJed Brown 15303f2090d5SJed Brown PetscFunctionBegin; 15310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 153272ac3e02SJed Brown if (ts->ops->poststep) { 15333f2090d5SJed Brown PetscStackPush("TS PostStep function"); 15343f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 15353f2090d5SJed Brown PetscStackPop; 153672ac3e02SJed Brown } 15373f2090d5SJed Brown PetscFunctionReturn(0); 15383f2090d5SJed Brown } 15393f2090d5SJed Brown 1540d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1541d763cef2SBarry Smith 15424a2ae208SSatish Balay #undef __FUNCT__ 1543a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1544d763cef2SBarry Smith /*@C 1545a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1546d763cef2SBarry Smith timestep to display the iteration's progress. 1547d763cef2SBarry Smith 15483f9fe445SBarry Smith Logically Collective on TS 1549d763cef2SBarry Smith 1550d763cef2SBarry Smith Input Parameters: 1551d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1552d763cef2SBarry Smith . func - monitoring routine 1553329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1554b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1555b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1556b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1557d763cef2SBarry Smith 1558d763cef2SBarry Smith Calling sequence of func: 1559a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1560d763cef2SBarry Smith 1561d763cef2SBarry Smith + ts - the TS context 1562d763cef2SBarry Smith . steps - iteration number 15631f06c33eSBarry Smith . time - current time 1564d763cef2SBarry Smith . x - current iterate 1565d763cef2SBarry Smith - mctx - [optional] monitoring context 1566d763cef2SBarry Smith 1567d763cef2SBarry Smith Notes: 1568d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1569d763cef2SBarry Smith already has been loaded. 1570d763cef2SBarry Smith 1571025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1572025f1a04SBarry Smith 1573d763cef2SBarry Smith Level: intermediate 1574d763cef2SBarry Smith 1575d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1576d763cef2SBarry Smith 1577a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1578d763cef2SBarry Smith @*/ 1579c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1580d763cef2SBarry Smith { 1581d763cef2SBarry Smith PetscFunctionBegin; 15820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 158317186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1584d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1585329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1586d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1587d763cef2SBarry Smith PetscFunctionReturn(0); 1588d763cef2SBarry Smith } 1589d763cef2SBarry Smith 15904a2ae208SSatish Balay #undef __FUNCT__ 1591a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1592d763cef2SBarry Smith /*@C 1593a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1594d763cef2SBarry Smith 15953f9fe445SBarry Smith Logically Collective on TS 1596d763cef2SBarry Smith 1597d763cef2SBarry Smith Input Parameters: 1598d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1599d763cef2SBarry Smith 1600d763cef2SBarry Smith Notes: 1601d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1602d763cef2SBarry Smith 1603d763cef2SBarry Smith Level: intermediate 1604d763cef2SBarry Smith 1605d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1606d763cef2SBarry Smith 1607a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1608d763cef2SBarry Smith @*/ 16097087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1610d763cef2SBarry Smith { 1611d952e501SBarry Smith PetscErrorCode ierr; 1612d952e501SBarry Smith PetscInt i; 1613d952e501SBarry Smith 1614d763cef2SBarry Smith PetscFunctionBegin; 16150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1616d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1617d952e501SBarry Smith if (ts->mdestroy[i]) { 16183c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1619d952e501SBarry Smith } 1620d952e501SBarry Smith } 1621d763cef2SBarry Smith ts->numbermonitors = 0; 1622d763cef2SBarry Smith PetscFunctionReturn(0); 1623d763cef2SBarry Smith } 1624d763cef2SBarry Smith 16254a2ae208SSatish Balay #undef __FUNCT__ 1626a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1627d8e5e3e6SSatish Balay /*@ 1628a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16295516499fSSatish Balay 16305516499fSSatish Balay Level: intermediate 163141251cbbSSatish Balay 16325516499fSSatish Balay .keywords: TS, set, monitor 16335516499fSSatish Balay 163441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 163541251cbbSSatish Balay @*/ 1636649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1637d763cef2SBarry Smith { 1638dfbe8321SBarry Smith PetscErrorCode ierr; 1639649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1640d132466eSBarry Smith 1641d763cef2SBarry Smith PetscFunctionBegin; 1642649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1643649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1644649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1645d763cef2SBarry Smith PetscFunctionReturn(0); 1646d763cef2SBarry Smith } 1647d763cef2SBarry Smith 16484a2ae208SSatish Balay #undef __FUNCT__ 16494a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1650d763cef2SBarry Smith /*@ 1651d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1652d763cef2SBarry Smith 1653d763cef2SBarry Smith Collective on TS 1654d763cef2SBarry Smith 1655d763cef2SBarry Smith Input Parameter: 1656d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1657d763cef2SBarry Smith 1658d763cef2SBarry Smith Output Parameters: 1659d763cef2SBarry Smith + steps - number of iterations until termination 1660142b95e3SSatish Balay - ptime - time until termination 1661d763cef2SBarry Smith 1662d763cef2SBarry Smith Level: beginner 1663d763cef2SBarry Smith 1664d763cef2SBarry Smith .keywords: TS, timestep, solve 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1667d763cef2SBarry Smith @*/ 1668193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1669d763cef2SBarry Smith { 1670dfbe8321SBarry Smith PetscErrorCode ierr; 1671d763cef2SBarry Smith 1672d763cef2SBarry Smith PetscFunctionBegin; 16730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1674277b19d0SLisandro Dalcin 1675d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1676d405a339SMatthew Knepley 1677d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1678193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1679d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1680d763cef2SBarry Smith PetscFunctionReturn(0); 1681d763cef2SBarry Smith } 1682d763cef2SBarry Smith 16834a2ae208SSatish Balay #undef __FUNCT__ 16846a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 16856a4d4014SLisandro Dalcin /*@ 16866a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 16876a4d4014SLisandro Dalcin 16886a4d4014SLisandro Dalcin Collective on TS 16896a4d4014SLisandro Dalcin 16906a4d4014SLisandro Dalcin Input Parameter: 16916a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 16926a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 16936a4d4014SLisandro Dalcin 16946a4d4014SLisandro Dalcin Level: beginner 16956a4d4014SLisandro Dalcin 16966a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 16976a4d4014SLisandro Dalcin 16986a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 16996a4d4014SLisandro Dalcin @*/ 17007087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17016a4d4014SLisandro Dalcin { 1702193ac0bcSJed Brown PetscInt i; 17034d7d938eSLisandro Dalcin PetscBool flg; 17044d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 17054d7d938eSLisandro Dalcin PetscViewer viewer; 17066a4d4014SLisandro Dalcin PetscErrorCode ierr; 1707f22f69f0SBarry Smith 17086a4d4014SLisandro Dalcin PetscFunctionBegin; 17090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1710193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1711193ac0bcSJed Brown ierr = TSSetSolution(ts,x); CHKERRQ(ierr); 1712b5d403baSSean Farley ierr = TSSetUp(ts); CHKERRQ(ierr); 17136a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1714193ac0bcSJed Brown ts->steps = 0; 1715193ac0bcSJed Brown ts->linear_its = 0; 1716193ac0bcSJed Brown ts->nonlinear_its = 0; 1717193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1718193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1719193ac0bcSJed Brown 1720193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1721193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 1722193ac0bcSJed Brown } else { 1723e1a7a14fSJed Brown i = 0; 1724e1a7a14fSJed Brown if (i >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 1725e1a7a14fSJed Brown else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 17266a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 1727e1a7a14fSJed Brown while (!ts->reason) { 1728193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1729193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1730193ac0bcSJed Brown if (ts->reason < 0) { 1731193ac0bcSJed Brown if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed"); 17323daf2de8SJed Brown } else if (++i >= ts->max_steps) { 1733193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITS; 1734193ac0bcSJed Brown } else if (ts->ptime >= ts->max_time) { 1735193ac0bcSJed Brown ts->reason = TS_CONVERGED_TIME; 1736193ac0bcSJed Brown } 1737193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1738193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1739193ac0bcSJed Brown } 1740193ac0bcSJed Brown } 17414d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 17424d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 17434d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 17444d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 17454d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1746193ac0bcSJed Brown } 17476a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17486a4d4014SLisandro Dalcin } 17496a4d4014SLisandro Dalcin 17506a4d4014SLisandro Dalcin #undef __FUNCT__ 17514a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1752d763cef2SBarry Smith /* 1753d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1754d763cef2SBarry Smith */ 1755a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1756d763cef2SBarry Smith { 17576849ba73SBarry Smith PetscErrorCode ierr; 1758a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1759d763cef2SBarry Smith 1760d763cef2SBarry Smith PetscFunctionBegin; 1761d763cef2SBarry Smith for (i=0; i<n; i++) { 1762142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1763d763cef2SBarry Smith } 1764d763cef2SBarry Smith PetscFunctionReturn(0); 1765d763cef2SBarry Smith } 1766d763cef2SBarry Smith 1767d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1768d763cef2SBarry Smith 17694a2ae208SSatish Balay #undef __FUNCT__ 1770a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1771d763cef2SBarry Smith /*@C 1772a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1773d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1774d763cef2SBarry Smith 1775d763cef2SBarry Smith Collective on TS 1776d763cef2SBarry Smith 1777d763cef2SBarry Smith Input Parameters: 1778d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1779d763cef2SBarry Smith . label - the title to put in the title bar 17807c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1781d763cef2SBarry Smith - m, n - the screen width and height in pixels 1782d763cef2SBarry Smith 1783d763cef2SBarry Smith Output Parameter: 1784d763cef2SBarry Smith . draw - the drawing context 1785d763cef2SBarry Smith 1786d763cef2SBarry Smith Options Database Key: 1787a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1788d763cef2SBarry Smith 1789d763cef2SBarry Smith Notes: 1790a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1791d763cef2SBarry Smith 1792d763cef2SBarry Smith Level: intermediate 1793d763cef2SBarry Smith 17947c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1795d763cef2SBarry Smith 1796a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 17977c922b88SBarry Smith 1798d763cef2SBarry Smith @*/ 17997087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1800d763cef2SBarry Smith { 1801b0a32e0cSBarry Smith PetscDraw win; 1802dfbe8321SBarry Smith PetscErrorCode ierr; 1803d763cef2SBarry Smith 1804d763cef2SBarry Smith PetscFunctionBegin; 1805b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1806b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1807b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1808b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1809d763cef2SBarry Smith 181052e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1811d763cef2SBarry Smith PetscFunctionReturn(0); 1812d763cef2SBarry Smith } 1813d763cef2SBarry Smith 18144a2ae208SSatish Balay #undef __FUNCT__ 1815a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1816a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1817d763cef2SBarry Smith { 1818b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 181987828ca2SBarry Smith PetscReal x,y = ptime; 1820dfbe8321SBarry Smith PetscErrorCode ierr; 1821d763cef2SBarry Smith 1822d763cef2SBarry Smith PetscFunctionBegin; 18237c922b88SBarry Smith if (!monctx) { 18247c922b88SBarry Smith MPI_Comm comm; 1825b0a32e0cSBarry Smith PetscViewer viewer; 18267c922b88SBarry Smith 18277c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1828b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1829b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18307c922b88SBarry Smith } 18317c922b88SBarry Smith 1832b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 183387828ca2SBarry Smith x = (PetscReal)n; 1834b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1835d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1836b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1837d763cef2SBarry Smith } 1838d763cef2SBarry Smith PetscFunctionReturn(0); 1839d763cef2SBarry Smith } 1840d763cef2SBarry Smith 18414a2ae208SSatish Balay #undef __FUNCT__ 1842a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1843d763cef2SBarry Smith /*@C 1844a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1845a6570f20SBarry Smith with TSMonitorLGCreate(). 1846d763cef2SBarry Smith 1847b0a32e0cSBarry Smith Collective on PetscDrawLG 1848d763cef2SBarry Smith 1849d763cef2SBarry Smith Input Parameter: 1850d763cef2SBarry Smith . draw - the drawing context 1851d763cef2SBarry Smith 1852d763cef2SBarry Smith Level: intermediate 1853d763cef2SBarry Smith 1854d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1855d763cef2SBarry Smith 1856a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1857d763cef2SBarry Smith @*/ 18586bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1859d763cef2SBarry Smith { 1860b0a32e0cSBarry Smith PetscDraw draw; 1861dfbe8321SBarry Smith PetscErrorCode ierr; 1862d763cef2SBarry Smith 1863d763cef2SBarry Smith PetscFunctionBegin; 18646bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 18656bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1866b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1867d763cef2SBarry Smith PetscFunctionReturn(0); 1868d763cef2SBarry Smith } 1869d763cef2SBarry Smith 18704a2ae208SSatish Balay #undef __FUNCT__ 18714a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1872d763cef2SBarry Smith /*@ 1873d763cef2SBarry Smith TSGetTime - Gets the current time. 1874d763cef2SBarry Smith 1875d763cef2SBarry Smith Not Collective 1876d763cef2SBarry Smith 1877d763cef2SBarry Smith Input Parameter: 1878d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith Output Parameter: 1881d763cef2SBarry Smith . t - the current time 1882d763cef2SBarry Smith 1883d763cef2SBarry Smith Level: beginner 1884d763cef2SBarry Smith 1885d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1886d763cef2SBarry Smith 1887d763cef2SBarry Smith .keywords: TS, get, time 1888d763cef2SBarry Smith @*/ 18897087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1890d763cef2SBarry Smith { 1891d763cef2SBarry Smith PetscFunctionBegin; 18920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18934482741eSBarry Smith PetscValidDoublePointer(t,2); 1894d763cef2SBarry Smith *t = ts->ptime; 1895d763cef2SBarry Smith PetscFunctionReturn(0); 1896d763cef2SBarry Smith } 1897d763cef2SBarry Smith 18984a2ae208SSatish Balay #undef __FUNCT__ 18996a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19006a4d4014SLisandro Dalcin /*@ 19016a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19026a4d4014SLisandro Dalcin 19033f9fe445SBarry Smith Logically Collective on TS 19046a4d4014SLisandro Dalcin 19056a4d4014SLisandro Dalcin Input Parameters: 19066a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19076a4d4014SLisandro Dalcin - time - the time 19086a4d4014SLisandro Dalcin 19096a4d4014SLisandro Dalcin Level: intermediate 19106a4d4014SLisandro Dalcin 19116a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19126a4d4014SLisandro Dalcin 19136a4d4014SLisandro Dalcin .keywords: TS, set, time 19146a4d4014SLisandro Dalcin @*/ 19157087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19166a4d4014SLisandro Dalcin { 19176a4d4014SLisandro Dalcin PetscFunctionBegin; 19180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1919c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19206a4d4014SLisandro Dalcin ts->ptime = t; 19216a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19226a4d4014SLisandro Dalcin } 19236a4d4014SLisandro Dalcin 19246a4d4014SLisandro Dalcin #undef __FUNCT__ 19254a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1926d763cef2SBarry Smith /*@C 1927d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1928d763cef2SBarry Smith TS options in the database. 1929d763cef2SBarry Smith 19303f9fe445SBarry Smith Logically Collective on TS 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith Input Parameter: 1933d763cef2SBarry Smith + ts - The TS context 1934d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1935d763cef2SBarry Smith 1936d763cef2SBarry Smith Notes: 1937d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1938d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1939d763cef2SBarry Smith hyphen. 1940d763cef2SBarry Smith 1941d763cef2SBarry Smith Level: advanced 1942d763cef2SBarry Smith 1943d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1944d763cef2SBarry Smith 1945d763cef2SBarry Smith .seealso: TSSetFromOptions() 1946d763cef2SBarry Smith 1947d763cef2SBarry Smith @*/ 19487087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1949d763cef2SBarry Smith { 1950dfbe8321SBarry Smith PetscErrorCode ierr; 1951089b2837SJed Brown SNES snes; 1952d763cef2SBarry Smith 1953d763cef2SBarry Smith PetscFunctionBegin; 19540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1955d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1956089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1957089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 1958d763cef2SBarry Smith PetscFunctionReturn(0); 1959d763cef2SBarry Smith } 1960d763cef2SBarry Smith 1961d763cef2SBarry Smith 19624a2ae208SSatish Balay #undef __FUNCT__ 19634a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 1964d763cef2SBarry Smith /*@C 1965d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 1966d763cef2SBarry Smith TS options in the database. 1967d763cef2SBarry Smith 19683f9fe445SBarry Smith Logically Collective on TS 1969d763cef2SBarry Smith 1970d763cef2SBarry Smith Input Parameter: 1971d763cef2SBarry Smith + ts - The TS context 1972d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1973d763cef2SBarry Smith 1974d763cef2SBarry Smith Notes: 1975d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1976d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1977d763cef2SBarry Smith hyphen. 1978d763cef2SBarry Smith 1979d763cef2SBarry Smith Level: advanced 1980d763cef2SBarry Smith 1981d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 1982d763cef2SBarry Smith 1983d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 1984d763cef2SBarry Smith 1985d763cef2SBarry Smith @*/ 19867087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 1987d763cef2SBarry Smith { 1988dfbe8321SBarry Smith PetscErrorCode ierr; 1989089b2837SJed Brown SNES snes; 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith PetscFunctionBegin; 19920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1993d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 1994089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1995089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 1996d763cef2SBarry Smith PetscFunctionReturn(0); 1997d763cef2SBarry Smith } 1998d763cef2SBarry Smith 19994a2ae208SSatish Balay #undef __FUNCT__ 20004a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2001d763cef2SBarry Smith /*@C 2002d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2003d763cef2SBarry Smith TS options in the database. 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith Not Collective 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith Input Parameter: 2008d763cef2SBarry Smith . ts - The TS context 2009d763cef2SBarry Smith 2010d763cef2SBarry Smith Output Parameter: 2011d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2014d763cef2SBarry Smith sufficient length to hold the prefix. 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith Level: intermediate 2017d763cef2SBarry Smith 2018d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2019d763cef2SBarry Smith 2020d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2021d763cef2SBarry Smith @*/ 20227087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2023d763cef2SBarry Smith { 2024dfbe8321SBarry Smith PetscErrorCode ierr; 2025d763cef2SBarry Smith 2026d763cef2SBarry Smith PetscFunctionBegin; 20270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20284482741eSBarry Smith PetscValidPointer(prefix,2); 2029d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2030d763cef2SBarry Smith PetscFunctionReturn(0); 2031d763cef2SBarry Smith } 2032d763cef2SBarry Smith 20334a2ae208SSatish Balay #undef __FUNCT__ 20344a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2035d763cef2SBarry Smith /*@C 2036d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2037d763cef2SBarry Smith 2038d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Input Parameter: 2041d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith Output Parameters: 2044d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2045d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2046089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2047d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2048d763cef2SBarry Smith 2049d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Level: intermediate 2052d763cef2SBarry Smith 205326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2056d763cef2SBarry Smith @*/ 2057089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2058d763cef2SBarry Smith { 2059089b2837SJed Brown PetscErrorCode ierr; 2060089b2837SJed Brown SNES snes; 2061089b2837SJed Brown 2062d763cef2SBarry Smith PetscFunctionBegin; 2063089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2064089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 20653b5f76d0SSean Farley if (func) *func = ts->userops->rhsjacobian; 206626d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2067d763cef2SBarry Smith PetscFunctionReturn(0); 2068d763cef2SBarry Smith } 2069d763cef2SBarry Smith 20701713a123SBarry Smith #undef __FUNCT__ 20712eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 20722eca1d9cSJed Brown /*@C 20732eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 20742eca1d9cSJed Brown 20752eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 20762eca1d9cSJed Brown 20772eca1d9cSJed Brown Input Parameter: 20782eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 20792eca1d9cSJed Brown 20802eca1d9cSJed Brown Output Parameters: 20812eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 20822eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 20832eca1d9cSJed Brown . f - The function to compute the matrices 20842eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 20852eca1d9cSJed Brown 20862eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 20872eca1d9cSJed Brown 20882eca1d9cSJed Brown Level: advanced 20892eca1d9cSJed Brown 20902eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 20912eca1d9cSJed Brown 20922eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 20932eca1d9cSJed Brown @*/ 20947087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 20952eca1d9cSJed Brown { 2096089b2837SJed Brown PetscErrorCode ierr; 2097089b2837SJed Brown SNES snes; 2098089b2837SJed Brown 20992eca1d9cSJed Brown PetscFunctionBegin; 2100089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2101089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21023b5f76d0SSean Farley if (f) *f = ts->userops->ijacobian; 21032eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21042eca1d9cSJed Brown PetscFunctionReturn(0); 21052eca1d9cSJed Brown } 21062eca1d9cSJed Brown 21072eca1d9cSJed Brown #undef __FUNCT__ 2108a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21091713a123SBarry Smith /*@C 2110a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21111713a123SBarry Smith VecView() for the solution at each timestep 21121713a123SBarry Smith 21131713a123SBarry Smith Collective on TS 21141713a123SBarry Smith 21151713a123SBarry Smith Input Parameters: 21161713a123SBarry Smith + ts - the TS context 21171713a123SBarry Smith . step - current time-step 2118142b95e3SSatish Balay . ptime - current time 21191713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21201713a123SBarry Smith 21211713a123SBarry Smith Level: intermediate 21221713a123SBarry Smith 21231713a123SBarry Smith .keywords: TS, vector, monitor, view 21241713a123SBarry Smith 2125a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21261713a123SBarry Smith @*/ 21277087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21281713a123SBarry Smith { 2129dfbe8321SBarry Smith PetscErrorCode ierr; 21301713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21311713a123SBarry Smith 21321713a123SBarry Smith PetscFunctionBegin; 2133a34d58ebSBarry Smith if (!dummy) { 21347adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21351713a123SBarry Smith } 21361713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21371713a123SBarry Smith PetscFunctionReturn(0); 21381713a123SBarry Smith } 21391713a123SBarry Smith 21401713a123SBarry Smith 21416c699258SBarry Smith #undef __FUNCT__ 21426c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21436c699258SBarry Smith /*@ 21446c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21456c699258SBarry Smith 21463f9fe445SBarry Smith Logically Collective on TS and DM 21476c699258SBarry Smith 21486c699258SBarry Smith Input Parameters: 21496c699258SBarry Smith + ts - the preconditioner context 21506c699258SBarry Smith - dm - the dm 21516c699258SBarry Smith 21526c699258SBarry Smith Level: intermediate 21536c699258SBarry Smith 21546c699258SBarry Smith 21556c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 21566c699258SBarry Smith @*/ 21577087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 21586c699258SBarry Smith { 21596c699258SBarry Smith PetscErrorCode ierr; 2160089b2837SJed Brown SNES snes; 21616c699258SBarry Smith 21626c699258SBarry Smith PetscFunctionBegin; 21630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 216470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 21656bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 21666c699258SBarry Smith ts->dm = dm; 2167089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2168089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 21696c699258SBarry Smith PetscFunctionReturn(0); 21706c699258SBarry Smith } 21716c699258SBarry Smith 21726c699258SBarry Smith #undef __FUNCT__ 21736c699258SBarry Smith #define __FUNCT__ "TSGetDM" 21746c699258SBarry Smith /*@ 21756c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 21766c699258SBarry Smith 21773f9fe445SBarry Smith Not Collective 21786c699258SBarry Smith 21796c699258SBarry Smith Input Parameter: 21806c699258SBarry Smith . ts - the preconditioner context 21816c699258SBarry Smith 21826c699258SBarry Smith Output Parameter: 21836c699258SBarry Smith . dm - the dm 21846c699258SBarry Smith 21856c699258SBarry Smith Level: intermediate 21866c699258SBarry Smith 21876c699258SBarry Smith 21886c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 21896c699258SBarry Smith @*/ 21907087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 21916c699258SBarry Smith { 21926c699258SBarry Smith PetscFunctionBegin; 21930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21946c699258SBarry Smith *dm = ts->dm; 21956c699258SBarry Smith PetscFunctionReturn(0); 21966c699258SBarry Smith } 21971713a123SBarry Smith 21980f5c6efeSJed Brown #undef __FUNCT__ 21990f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22000f5c6efeSJed Brown /*@ 22010f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22020f5c6efeSJed Brown 22033f9fe445SBarry Smith Logically Collective on SNES 22040f5c6efeSJed Brown 22050f5c6efeSJed Brown Input Parameter: 2206d42a1c89SJed Brown + snes - nonlinear solver 22070f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2208d42a1c89SJed Brown - ctx - user context, must be a TS 22090f5c6efeSJed Brown 22100f5c6efeSJed Brown Output Parameter: 22110f5c6efeSJed Brown . F - the nonlinear residual 22120f5c6efeSJed Brown 22130f5c6efeSJed Brown Notes: 22140f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22150f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22160f5c6efeSJed Brown 22170f5c6efeSJed Brown Level: advanced 22180f5c6efeSJed Brown 22190f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22200f5c6efeSJed Brown @*/ 22217087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22220f5c6efeSJed Brown { 22230f5c6efeSJed Brown TS ts = (TS)ctx; 22240f5c6efeSJed Brown PetscErrorCode ierr; 22250f5c6efeSJed Brown 22260f5c6efeSJed Brown PetscFunctionBegin; 22270f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22280f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22290f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22300f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22310f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22320f5c6efeSJed Brown PetscFunctionReturn(0); 22330f5c6efeSJed Brown } 22340f5c6efeSJed Brown 22350f5c6efeSJed Brown #undef __FUNCT__ 22360f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22370f5c6efeSJed Brown /*@ 22380f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22390f5c6efeSJed Brown 22400f5c6efeSJed Brown Collective on SNES 22410f5c6efeSJed Brown 22420f5c6efeSJed Brown Input Parameter: 22430f5c6efeSJed Brown + snes - nonlinear solver 22440f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22450f5c6efeSJed Brown - ctx - user context, must be a TS 22460f5c6efeSJed Brown 22470f5c6efeSJed Brown Output Parameter: 22480f5c6efeSJed Brown + A - the Jacobian 22490f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 22500f5c6efeSJed Brown - flag - indicates any structure change in the matrix 22510f5c6efeSJed Brown 22520f5c6efeSJed Brown Notes: 22530f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22540f5c6efeSJed Brown 22550f5c6efeSJed Brown Level: developer 22560f5c6efeSJed Brown 22570f5c6efeSJed Brown .seealso: SNESSetJacobian() 22580f5c6efeSJed Brown @*/ 22597087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 22600f5c6efeSJed Brown { 22610f5c6efeSJed Brown TS ts = (TS)ctx; 22620f5c6efeSJed Brown PetscErrorCode ierr; 22630f5c6efeSJed Brown 22640f5c6efeSJed Brown PetscFunctionBegin; 22650f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22660f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22670f5c6efeSJed Brown PetscValidPointer(A,3); 22680f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 22690f5c6efeSJed Brown PetscValidPointer(B,4); 22700f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 22710f5c6efeSJed Brown PetscValidPointer(flag,5); 22720f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 22730f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 22740f5c6efeSJed Brown PetscFunctionReturn(0); 22750f5c6efeSJed Brown } 2276325fc9f4SBarry Smith 22770e4ef248SJed Brown #undef __FUNCT__ 22780e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 22790e4ef248SJed Brown /*@C 22800e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 22810e4ef248SJed Brown 22820e4ef248SJed Brown Collective on TS 22830e4ef248SJed Brown 22840e4ef248SJed Brown Input Arguments: 22850e4ef248SJed Brown + ts - time stepping context 22860e4ef248SJed Brown . t - time at which to evaluate 22870e4ef248SJed Brown . X - state at which to evaluate 22880e4ef248SJed Brown - ctx - context 22890e4ef248SJed Brown 22900e4ef248SJed Brown Output Arguments: 22910e4ef248SJed Brown . F - right hand side 22920e4ef248SJed Brown 22930e4ef248SJed Brown Level: intermediate 22940e4ef248SJed Brown 22950e4ef248SJed Brown Notes: 22960e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 22970e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 22980e4ef248SJed Brown 22990e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 23000e4ef248SJed Brown @*/ 23010e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 23020e4ef248SJed Brown { 23030e4ef248SJed Brown PetscErrorCode ierr; 23040e4ef248SJed Brown Mat Arhs,Brhs; 23050e4ef248SJed Brown MatStructure flg2; 23060e4ef248SJed Brown 23070e4ef248SJed Brown PetscFunctionBegin; 23080e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 23090e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 23100e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 23110e4ef248SJed Brown PetscFunctionReturn(0); 23120e4ef248SJed Brown } 23130e4ef248SJed Brown 23140e4ef248SJed Brown #undef __FUNCT__ 23150e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 23160e4ef248SJed Brown /*@C 23170e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 23180e4ef248SJed Brown 23190e4ef248SJed Brown Collective on TS 23200e4ef248SJed Brown 23210e4ef248SJed Brown Input Arguments: 23220e4ef248SJed Brown + ts - time stepping context 23230e4ef248SJed Brown . t - time at which to evaluate 23240e4ef248SJed Brown . X - state at which to evaluate 23250e4ef248SJed Brown - ctx - context 23260e4ef248SJed Brown 23270e4ef248SJed Brown Output Arguments: 23280e4ef248SJed Brown + A - pointer to operator 23290e4ef248SJed Brown . B - pointer to preconditioning matrix 23300e4ef248SJed Brown - flg - matrix structure flag 23310e4ef248SJed Brown 23320e4ef248SJed Brown Level: intermediate 23330e4ef248SJed Brown 23340e4ef248SJed Brown Notes: 23350e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 23360e4ef248SJed Brown 23370e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 23380e4ef248SJed Brown @*/ 23390e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 23400e4ef248SJed Brown { 23410e4ef248SJed Brown 23420e4ef248SJed Brown PetscFunctionBegin; 23430e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 23440e4ef248SJed Brown PetscFunctionReturn(0); 23450e4ef248SJed Brown } 23460e4ef248SJed Brown 23470026cea9SSean Farley #undef __FUNCT__ 23480026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 23490026cea9SSean Farley /*@C 23500026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 23510026cea9SSean Farley 23520026cea9SSean Farley Collective on TS 23530026cea9SSean Farley 23540026cea9SSean Farley Input Arguments: 23550026cea9SSean Farley + ts - time stepping context 23560026cea9SSean Farley . t - time at which to evaluate 23570026cea9SSean Farley . X - state at which to evaluate 23580026cea9SSean Farley . Xdot - time derivative of state vector 23590026cea9SSean Farley - ctx - context 23600026cea9SSean Farley 23610026cea9SSean Farley Output Arguments: 23620026cea9SSean Farley . F - left hand side 23630026cea9SSean Farley 23640026cea9SSean Farley Level: intermediate 23650026cea9SSean Farley 23660026cea9SSean Farley Notes: 23670026cea9SSean Farley The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the 23680026cea9SSean Farley user is required to write their own TSComputeIFunction. 23690026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 23700026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 23710026cea9SSean Farley 23720026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 23730026cea9SSean Farley @*/ 23740026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 23750026cea9SSean Farley { 23760026cea9SSean Farley PetscErrorCode ierr; 23770026cea9SSean Farley Mat A,B; 23780026cea9SSean Farley MatStructure flg2; 23790026cea9SSean Farley 23800026cea9SSean Farley PetscFunctionBegin; 23810026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 23820026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 23830026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 23840026cea9SSean Farley PetscFunctionReturn(0); 23850026cea9SSean Farley } 23860026cea9SSean Farley 23870026cea9SSean Farley #undef __FUNCT__ 23880026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 23890026cea9SSean Farley /*@C 23900026cea9SSean Farley TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 23910026cea9SSean Farley 23920026cea9SSean Farley Collective on TS 23930026cea9SSean Farley 23940026cea9SSean Farley Input Arguments: 23950026cea9SSean Farley + ts - time stepping context 23960026cea9SSean Farley . t - time at which to evaluate 23970026cea9SSean Farley . X - state at which to evaluate 23980026cea9SSean Farley . Xdot - time derivative of state vector 23990026cea9SSean Farley . shift - shift to apply 24000026cea9SSean Farley - ctx - context 24010026cea9SSean Farley 24020026cea9SSean Farley Output Arguments: 24030026cea9SSean Farley + A - pointer to operator 24040026cea9SSean Farley . B - pointer to preconditioning matrix 24050026cea9SSean Farley - flg - matrix structure flag 24060026cea9SSean Farley 24070026cea9SSean Farley Level: intermediate 24080026cea9SSean Farley 24090026cea9SSean Farley Notes: 24100026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 24110026cea9SSean Farley 24120026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 24130026cea9SSean Farley @*/ 24140026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 24150026cea9SSean Farley { 24160026cea9SSean Farley 24170026cea9SSean Farley PetscFunctionBegin; 24180026cea9SSean Farley *flg = SAME_PRECONDITIONER; 24190026cea9SSean Farley PetscFunctionReturn(0); 24200026cea9SSean Farley } 24210026cea9SSean Farley 24224af1b03aSJed Brown 24234af1b03aSJed Brown #undef __FUNCT__ 24244af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 24254af1b03aSJed Brown /*@ 24264af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 24274af1b03aSJed Brown 24284af1b03aSJed Brown Not Collective 24294af1b03aSJed Brown 24304af1b03aSJed Brown Input Parameter: 24314af1b03aSJed Brown . ts - the TS context 24324af1b03aSJed Brown 24334af1b03aSJed Brown Output Parameter: 24344af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 24354af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 24364af1b03aSJed Brown 24374af1b03aSJed Brown Level: intermediate 24384af1b03aSJed Brown 24394af1b03aSJed Brown Notes: Can only be called after the call to TSSolve() is complete. 24404af1b03aSJed Brown 24414af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 24424af1b03aSJed Brown 24434af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 24444af1b03aSJed Brown @*/ 24454af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 24464af1b03aSJed Brown { 24474af1b03aSJed Brown PetscFunctionBegin; 24484af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24494af1b03aSJed Brown PetscValidPointer(reason,2); 24504af1b03aSJed Brown *reason = ts->reason; 24514af1b03aSJed Brown PetscFunctionReturn(0); 24524af1b03aSJed Brown } 24534af1b03aSJed Brown 2454325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2455c6db04a5SJed Brown #include <mex.h> 2456325fc9f4SBarry Smith 2457325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2458325fc9f4SBarry Smith 2459325fc9f4SBarry Smith #undef __FUNCT__ 2460325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2461325fc9f4SBarry Smith /* 2462325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2463325fc9f4SBarry Smith TSSetFunctionMatlab(). 2464325fc9f4SBarry Smith 2465325fc9f4SBarry Smith Collective on TS 2466325fc9f4SBarry Smith 2467325fc9f4SBarry Smith Input Parameters: 2468325fc9f4SBarry Smith + snes - the TS context 2469325fc9f4SBarry Smith - x - input vector 2470325fc9f4SBarry Smith 2471325fc9f4SBarry Smith Output Parameter: 2472325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2473325fc9f4SBarry Smith 2474325fc9f4SBarry Smith Notes: 2475325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2476325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2477325fc9f4SBarry Smith themselves. 2478325fc9f4SBarry Smith 2479325fc9f4SBarry Smith Level: developer 2480325fc9f4SBarry Smith 2481325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2482325fc9f4SBarry Smith 2483325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2484325fc9f4SBarry Smith */ 24857087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2486325fc9f4SBarry Smith { 2487325fc9f4SBarry Smith PetscErrorCode ierr; 2488325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2489325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2490325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2491325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2492325fc9f4SBarry Smith 2493325fc9f4SBarry Smith PetscFunctionBegin; 2494325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2495325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2496325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2497325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2498325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2499325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2500325fc9f4SBarry Smith 2501325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2502325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2503880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2504325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2505325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2506325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2507325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2508325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2509325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2510325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2511325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2512325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2513325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2514325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2515325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2516325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2517325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2518325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2519325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2520325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2521325fc9f4SBarry Smith PetscFunctionReturn(0); 2522325fc9f4SBarry Smith } 2523325fc9f4SBarry Smith 2524325fc9f4SBarry Smith 2525325fc9f4SBarry Smith #undef __FUNCT__ 2526325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2527325fc9f4SBarry Smith /* 2528325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2529325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2530e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2531325fc9f4SBarry Smith 2532325fc9f4SBarry Smith Logically Collective on TS 2533325fc9f4SBarry Smith 2534325fc9f4SBarry Smith Input Parameters: 2535325fc9f4SBarry Smith + ts - the TS context 2536325fc9f4SBarry Smith - func - function evaluation routine 2537325fc9f4SBarry Smith 2538325fc9f4SBarry Smith Calling sequence of func: 2539325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2540325fc9f4SBarry Smith 2541325fc9f4SBarry Smith Level: beginner 2542325fc9f4SBarry Smith 2543325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2544325fc9f4SBarry Smith 2545325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2546325fc9f4SBarry Smith */ 2547*cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 2548325fc9f4SBarry Smith { 2549325fc9f4SBarry Smith PetscErrorCode ierr; 2550325fc9f4SBarry Smith TSMatlabContext *sctx; 2551325fc9f4SBarry Smith 2552325fc9f4SBarry Smith PetscFunctionBegin; 2553325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2554325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2555325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2556325fc9f4SBarry Smith /* 2557325fc9f4SBarry Smith This should work, but it doesn't 2558325fc9f4SBarry Smith sctx->ctx = ctx; 2559325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2560325fc9f4SBarry Smith */ 2561325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2562*cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2563325fc9f4SBarry Smith PetscFunctionReturn(0); 2564325fc9f4SBarry Smith } 2565325fc9f4SBarry Smith 2566325fc9f4SBarry Smith #undef __FUNCT__ 2567325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2568325fc9f4SBarry Smith /* 2569325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2570325fc9f4SBarry Smith TSSetJacobianMatlab(). 2571325fc9f4SBarry Smith 2572325fc9f4SBarry Smith Collective on TS 2573325fc9f4SBarry Smith 2574325fc9f4SBarry Smith Input Parameters: 2575*cdcf91faSSean Farley + ts - the TS context 2576325fc9f4SBarry Smith . x - input vector 2577325fc9f4SBarry Smith . A, B - the matrices 2578325fc9f4SBarry Smith - ctx - user context 2579325fc9f4SBarry Smith 2580325fc9f4SBarry Smith Output Parameter: 2581325fc9f4SBarry Smith . flag - structure of the matrix 2582325fc9f4SBarry Smith 2583325fc9f4SBarry Smith Level: developer 2584325fc9f4SBarry Smith 2585325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2586325fc9f4SBarry Smith 2587325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2588325fc9f4SBarry Smith @*/ 2589*cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2590325fc9f4SBarry Smith { 2591325fc9f4SBarry Smith PetscErrorCode ierr; 2592325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2593325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2594325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2595325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2596325fc9f4SBarry Smith 2597325fc9f4SBarry Smith PetscFunctionBegin; 2598*cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2599325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2600325fc9f4SBarry Smith 2601325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2602325fc9f4SBarry Smith 2603*cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 2604325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2605325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2606325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2607325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2608325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2609325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2610325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2611325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2612325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2613325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2614325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2615325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2616325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2617325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2618325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2619325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2620325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2621325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2622325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2623325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2624325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2625325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2626325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2627325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2628325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2629325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2630325fc9f4SBarry Smith PetscFunctionReturn(0); 2631325fc9f4SBarry Smith } 2632325fc9f4SBarry Smith 2633325fc9f4SBarry Smith 2634325fc9f4SBarry Smith #undef __FUNCT__ 2635325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2636325fc9f4SBarry Smith /* 2637325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2638e3c5b3baSBarry 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 2639325fc9f4SBarry Smith 2640325fc9f4SBarry Smith Logically Collective on TS 2641325fc9f4SBarry Smith 2642325fc9f4SBarry Smith Input Parameters: 2643*cdcf91faSSean Farley + ts - the TS context 2644325fc9f4SBarry Smith . A,B - Jacobian matrices 2645325fc9f4SBarry Smith . func - function evaluation routine 2646325fc9f4SBarry Smith - ctx - user context 2647325fc9f4SBarry Smith 2648325fc9f4SBarry Smith Calling sequence of func: 2649*cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2650325fc9f4SBarry Smith 2651325fc9f4SBarry Smith 2652325fc9f4SBarry Smith Level: developer 2653325fc9f4SBarry Smith 2654325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2655325fc9f4SBarry Smith 2656325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2657325fc9f4SBarry Smith */ 2658*cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 2659325fc9f4SBarry Smith { 2660325fc9f4SBarry Smith PetscErrorCode ierr; 2661325fc9f4SBarry Smith TSMatlabContext *sctx; 2662325fc9f4SBarry Smith 2663325fc9f4SBarry Smith PetscFunctionBegin; 2664325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2665325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2666325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2667325fc9f4SBarry Smith /* 2668325fc9f4SBarry Smith This should work, but it doesn't 2669325fc9f4SBarry Smith sctx->ctx = ctx; 2670325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2671325fc9f4SBarry Smith */ 2672325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2673*cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2674325fc9f4SBarry Smith PetscFunctionReturn(0); 2675325fc9f4SBarry Smith } 2676325fc9f4SBarry Smith 2677b5b1a830SBarry Smith #undef __FUNCT__ 2678b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2679b5b1a830SBarry Smith /* 2680b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2681b5b1a830SBarry Smith 2682b5b1a830SBarry Smith Collective on TS 2683b5b1a830SBarry Smith 2684b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2685b5b1a830SBarry Smith @*/ 2686*cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 2687b5b1a830SBarry Smith { 2688b5b1a830SBarry Smith PetscErrorCode ierr; 2689b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2690a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2691b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2692b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2693b5b1a830SBarry Smith 2694b5b1a830SBarry Smith PetscFunctionBegin; 2695*cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2696b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2697b5b1a830SBarry Smith 2698*cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 2699b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2700b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2701b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2702b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2703b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2704b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2705b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2706b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2707b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2708b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2709b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2710b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2711b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2712b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2713b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2714b5b1a830SBarry Smith PetscFunctionReturn(0); 2715b5b1a830SBarry Smith } 2716b5b1a830SBarry Smith 2717b5b1a830SBarry Smith 2718b5b1a830SBarry Smith #undef __FUNCT__ 2719b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2720b5b1a830SBarry Smith /* 2721b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2722b5b1a830SBarry Smith 2723b5b1a830SBarry Smith Level: developer 2724b5b1a830SBarry Smith 2725b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2726b5b1a830SBarry Smith 2727b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2728b5b1a830SBarry Smith */ 2729*cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 2730b5b1a830SBarry Smith { 2731b5b1a830SBarry Smith PetscErrorCode ierr; 2732b5b1a830SBarry Smith TSMatlabContext *sctx; 2733b5b1a830SBarry Smith 2734b5b1a830SBarry Smith PetscFunctionBegin; 2735b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2736b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2737b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2738b5b1a830SBarry Smith /* 2739b5b1a830SBarry Smith This should work, but it doesn't 2740b5b1a830SBarry Smith sctx->ctx = ctx; 2741b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2742b5b1a830SBarry Smith */ 2743b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2744*cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2745b5b1a830SBarry Smith PetscFunctionReturn(0); 2746b5b1a830SBarry Smith } 2747b5b1a830SBarry Smith 2748325fc9f4SBarry Smith #endif 2749