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__ 1649cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 1650cd652676SJed Brown /*@ 1651cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 1652cd652676SJed Brown 1653cd652676SJed Brown Logically Collective on TS 1654cd652676SJed Brown 1655cd652676SJed Brown Input Argument: 1656cd652676SJed Brown . ts - time stepping context 1657cd652676SJed Brown 1658cd652676SJed Brown Output Argument: 1659cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 1660cd652676SJed Brown 1661cd652676SJed Brown Level: intermediate 1662cd652676SJed Brown 1663cd652676SJed Brown .keywords: TS, set 1664cd652676SJed Brown 1665cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 1666cd652676SJed Brown @*/ 1667cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 1668cd652676SJed Brown { 1669cd652676SJed Brown 1670cd652676SJed Brown PetscFunctionBegin; 1671cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1672cd652676SJed Brown ts->retain_stages = flg; 1673cd652676SJed Brown PetscFunctionReturn(0); 1674cd652676SJed Brown } 1675cd652676SJed Brown 1676cd652676SJed Brown #undef __FUNCT__ 1677cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 1678cd652676SJed Brown /*@ 1679cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 1680cd652676SJed Brown 1681cd652676SJed Brown Collective on TS 1682cd652676SJed Brown 1683cd652676SJed Brown Input Argument: 1684cd652676SJed Brown + ts - time stepping context 1685cd652676SJed Brown - t - time to interpolate to 1686cd652676SJed Brown 1687cd652676SJed Brown Output Argument: 1688cd652676SJed Brown . X - state at given time 1689cd652676SJed Brown 1690cd652676SJed Brown Notes: 1691cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 1692cd652676SJed Brown 1693cd652676SJed Brown Level: intermediate 1694cd652676SJed Brown 1695cd652676SJed Brown Developer Notes: 1696cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 1697cd652676SJed Brown 1698cd652676SJed Brown .keywords: TS, set 1699cd652676SJed Brown 1700cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 1701cd652676SJed Brown @*/ 1702cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 1703cd652676SJed Brown { 1704cd652676SJed Brown PetscErrorCode ierr; 1705cd652676SJed Brown 1706cd652676SJed Brown PetscFunctionBegin; 1707cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1708cd652676SJed Brown if (t < ts->ptime - ts->time_step || ts->ptime < t) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step,ts->ptime); 1709cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 1710cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 1711cd652676SJed Brown PetscFunctionReturn(0); 1712cd652676SJed Brown } 1713cd652676SJed Brown 1714cd652676SJed Brown #undef __FUNCT__ 17154a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1716d763cef2SBarry Smith /*@ 1717d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1718d763cef2SBarry Smith 1719d763cef2SBarry Smith Collective on TS 1720d763cef2SBarry Smith 1721d763cef2SBarry Smith Input Parameter: 1722d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1723d763cef2SBarry Smith 1724d763cef2SBarry Smith Output Parameters: 1725d763cef2SBarry Smith + steps - number of iterations until termination 1726142b95e3SSatish Balay - ptime - time until termination 1727d763cef2SBarry Smith 1728d763cef2SBarry Smith Level: beginner 1729d763cef2SBarry Smith 1730d763cef2SBarry Smith .keywords: TS, timestep, solve 1731d763cef2SBarry Smith 1732d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1733d763cef2SBarry Smith @*/ 1734193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1735d763cef2SBarry Smith { 1736dfbe8321SBarry Smith PetscErrorCode ierr; 1737d763cef2SBarry Smith 1738d763cef2SBarry Smith PetscFunctionBegin; 17390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1740277b19d0SLisandro Dalcin 1741d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1742d405a339SMatthew Knepley 1743d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1744193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1745d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1746d763cef2SBarry Smith PetscFunctionReturn(0); 1747d763cef2SBarry Smith } 1748d763cef2SBarry Smith 17494a2ae208SSatish Balay #undef __FUNCT__ 17506a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17516a4d4014SLisandro Dalcin /*@ 17526a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17536a4d4014SLisandro Dalcin 17546a4d4014SLisandro Dalcin Collective on TS 17556a4d4014SLisandro Dalcin 17566a4d4014SLisandro Dalcin Input Parameter: 17576a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17586a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17596a4d4014SLisandro Dalcin 17606a4d4014SLisandro Dalcin Level: beginner 17616a4d4014SLisandro Dalcin 17626a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17636a4d4014SLisandro Dalcin 17646a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17656a4d4014SLisandro Dalcin @*/ 17667087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17676a4d4014SLisandro Dalcin { 1768193ac0bcSJed Brown PetscInt i; 17694d7d938eSLisandro Dalcin PetscBool flg; 17704d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 17714d7d938eSLisandro Dalcin PetscViewer viewer; 17726a4d4014SLisandro Dalcin PetscErrorCode ierr; 1773f22f69f0SBarry Smith 17746a4d4014SLisandro Dalcin PetscFunctionBegin; 17750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1776193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1777193ac0bcSJed Brown ierr = TSSetSolution(ts,x); CHKERRQ(ierr); 1778b5d403baSSean Farley ierr = TSSetUp(ts); CHKERRQ(ierr); 17796a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1780193ac0bcSJed Brown ts->steps = 0; 1781193ac0bcSJed Brown ts->linear_its = 0; 1782193ac0bcSJed Brown ts->nonlinear_its = 0; 1783193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1784193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1785193ac0bcSJed Brown 1786193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1787193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 1788193ac0bcSJed Brown } else { 1789e1a7a14fSJed Brown i = 0; 1790e1a7a14fSJed Brown if (i >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 1791e1a7a14fSJed Brown else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 17926a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 1793e1a7a14fSJed Brown while (!ts->reason) { 1794193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1795193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1796193ac0bcSJed Brown if (ts->reason < 0) { 1797193ac0bcSJed Brown if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed"); 17983daf2de8SJed Brown } else if (++i >= ts->max_steps) { 1799193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITS; 1800193ac0bcSJed Brown } else if (ts->ptime >= ts->max_time) { 1801193ac0bcSJed Brown ts->reason = TS_CONVERGED_TIME; 1802193ac0bcSJed Brown } 1803193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1804193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1805193ac0bcSJed Brown } 1806193ac0bcSJed Brown } 18074d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 18084d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 18094d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 18104d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 18114d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1812193ac0bcSJed Brown } 18136a4d4014SLisandro Dalcin PetscFunctionReturn(0); 18146a4d4014SLisandro Dalcin } 18156a4d4014SLisandro Dalcin 18166a4d4014SLisandro Dalcin #undef __FUNCT__ 18174a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1818d763cef2SBarry Smith /* 1819d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1820d763cef2SBarry Smith */ 1821a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1822d763cef2SBarry Smith { 18236849ba73SBarry Smith PetscErrorCode ierr; 1824a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1825d763cef2SBarry Smith 1826d763cef2SBarry Smith PetscFunctionBegin; 1827d763cef2SBarry Smith for (i=0; i<n; i++) { 1828142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1829d763cef2SBarry Smith } 1830d763cef2SBarry Smith PetscFunctionReturn(0); 1831d763cef2SBarry Smith } 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1834d763cef2SBarry Smith 18354a2ae208SSatish Balay #undef __FUNCT__ 1836a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1837d763cef2SBarry Smith /*@C 1838a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1839d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1840d763cef2SBarry Smith 1841d763cef2SBarry Smith Collective on TS 1842d763cef2SBarry Smith 1843d763cef2SBarry Smith Input Parameters: 1844d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1845d763cef2SBarry Smith . label - the title to put in the title bar 18467c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1847d763cef2SBarry Smith - m, n - the screen width and height in pixels 1848d763cef2SBarry Smith 1849d763cef2SBarry Smith Output Parameter: 1850d763cef2SBarry Smith . draw - the drawing context 1851d763cef2SBarry Smith 1852d763cef2SBarry Smith Options Database Key: 1853a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1854d763cef2SBarry Smith 1855d763cef2SBarry Smith Notes: 1856a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1857d763cef2SBarry Smith 1858d763cef2SBarry Smith Level: intermediate 1859d763cef2SBarry Smith 18607c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1861d763cef2SBarry Smith 1862a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18637c922b88SBarry Smith 1864d763cef2SBarry Smith @*/ 18657087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1866d763cef2SBarry Smith { 1867b0a32e0cSBarry Smith PetscDraw win; 1868dfbe8321SBarry Smith PetscErrorCode ierr; 1869d763cef2SBarry Smith 1870d763cef2SBarry Smith PetscFunctionBegin; 1871b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1872b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1873b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1874b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1875d763cef2SBarry Smith 187652e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1877d763cef2SBarry Smith PetscFunctionReturn(0); 1878d763cef2SBarry Smith } 1879d763cef2SBarry Smith 18804a2ae208SSatish Balay #undef __FUNCT__ 1881a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1882a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1883d763cef2SBarry Smith { 1884b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 188587828ca2SBarry Smith PetscReal x,y = ptime; 1886dfbe8321SBarry Smith PetscErrorCode ierr; 1887d763cef2SBarry Smith 1888d763cef2SBarry Smith PetscFunctionBegin; 18897c922b88SBarry Smith if (!monctx) { 18907c922b88SBarry Smith MPI_Comm comm; 1891b0a32e0cSBarry Smith PetscViewer viewer; 18927c922b88SBarry Smith 18937c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1894b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1895b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18967c922b88SBarry Smith } 18977c922b88SBarry Smith 1898b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 189987828ca2SBarry Smith x = (PetscReal)n; 1900b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1901d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1902b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1903d763cef2SBarry Smith } 1904d763cef2SBarry Smith PetscFunctionReturn(0); 1905d763cef2SBarry Smith } 1906d763cef2SBarry Smith 19074a2ae208SSatish Balay #undef __FUNCT__ 1908a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1909d763cef2SBarry Smith /*@C 1910a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1911a6570f20SBarry Smith with TSMonitorLGCreate(). 1912d763cef2SBarry Smith 1913b0a32e0cSBarry Smith Collective on PetscDrawLG 1914d763cef2SBarry Smith 1915d763cef2SBarry Smith Input Parameter: 1916d763cef2SBarry Smith . draw - the drawing context 1917d763cef2SBarry Smith 1918d763cef2SBarry Smith Level: intermediate 1919d763cef2SBarry Smith 1920d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1921d763cef2SBarry Smith 1922a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1923d763cef2SBarry Smith @*/ 19246bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1925d763cef2SBarry Smith { 1926b0a32e0cSBarry Smith PetscDraw draw; 1927dfbe8321SBarry Smith PetscErrorCode ierr; 1928d763cef2SBarry Smith 1929d763cef2SBarry Smith PetscFunctionBegin; 19306bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 19316bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1932b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1933d763cef2SBarry Smith PetscFunctionReturn(0); 1934d763cef2SBarry Smith } 1935d763cef2SBarry Smith 19364a2ae208SSatish Balay #undef __FUNCT__ 19374a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1938d763cef2SBarry Smith /*@ 1939d763cef2SBarry Smith TSGetTime - Gets the current time. 1940d763cef2SBarry Smith 1941d763cef2SBarry Smith Not Collective 1942d763cef2SBarry Smith 1943d763cef2SBarry Smith Input Parameter: 1944d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1945d763cef2SBarry Smith 1946d763cef2SBarry Smith Output Parameter: 1947d763cef2SBarry Smith . t - the current time 1948d763cef2SBarry Smith 1949d763cef2SBarry Smith Level: beginner 1950d763cef2SBarry Smith 1951d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1952d763cef2SBarry Smith 1953d763cef2SBarry Smith .keywords: TS, get, time 1954d763cef2SBarry Smith @*/ 19557087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1956d763cef2SBarry Smith { 1957d763cef2SBarry Smith PetscFunctionBegin; 19580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19594482741eSBarry Smith PetscValidDoublePointer(t,2); 1960d763cef2SBarry Smith *t = ts->ptime; 1961d763cef2SBarry Smith PetscFunctionReturn(0); 1962d763cef2SBarry Smith } 1963d763cef2SBarry Smith 19644a2ae208SSatish Balay #undef __FUNCT__ 19656a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19666a4d4014SLisandro Dalcin /*@ 19676a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19686a4d4014SLisandro Dalcin 19693f9fe445SBarry Smith Logically Collective on TS 19706a4d4014SLisandro Dalcin 19716a4d4014SLisandro Dalcin Input Parameters: 19726a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19736a4d4014SLisandro Dalcin - time - the time 19746a4d4014SLisandro Dalcin 19756a4d4014SLisandro Dalcin Level: intermediate 19766a4d4014SLisandro Dalcin 19776a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19786a4d4014SLisandro Dalcin 19796a4d4014SLisandro Dalcin .keywords: TS, set, time 19806a4d4014SLisandro Dalcin @*/ 19817087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19826a4d4014SLisandro Dalcin { 19836a4d4014SLisandro Dalcin PetscFunctionBegin; 19840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1985c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19866a4d4014SLisandro Dalcin ts->ptime = t; 19876a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19886a4d4014SLisandro Dalcin } 19896a4d4014SLisandro Dalcin 19906a4d4014SLisandro Dalcin #undef __FUNCT__ 19914a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1992d763cef2SBarry Smith /*@C 1993d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1994d763cef2SBarry Smith TS options in the database. 1995d763cef2SBarry Smith 19963f9fe445SBarry Smith Logically Collective on TS 1997d763cef2SBarry Smith 1998d763cef2SBarry Smith Input Parameter: 1999d763cef2SBarry Smith + ts - The TS context 2000d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2001d763cef2SBarry Smith 2002d763cef2SBarry Smith Notes: 2003d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2004d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2005d763cef2SBarry Smith hyphen. 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith Level: advanced 2008d763cef2SBarry Smith 2009d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith .seealso: TSSetFromOptions() 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith @*/ 20147087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2015d763cef2SBarry Smith { 2016dfbe8321SBarry Smith PetscErrorCode ierr; 2017089b2837SJed Brown SNES snes; 2018d763cef2SBarry Smith 2019d763cef2SBarry Smith PetscFunctionBegin; 20200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2021d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2022089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2023089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2024d763cef2SBarry Smith PetscFunctionReturn(0); 2025d763cef2SBarry Smith } 2026d763cef2SBarry Smith 2027d763cef2SBarry Smith 20284a2ae208SSatish Balay #undef __FUNCT__ 20294a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2030d763cef2SBarry Smith /*@C 2031d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2032d763cef2SBarry Smith TS options in the database. 2033d763cef2SBarry Smith 20343f9fe445SBarry Smith Logically Collective on TS 2035d763cef2SBarry Smith 2036d763cef2SBarry Smith Input Parameter: 2037d763cef2SBarry Smith + ts - The TS context 2038d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Notes: 2041d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2042d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2043d763cef2SBarry Smith hyphen. 2044d763cef2SBarry Smith 2045d763cef2SBarry Smith Level: advanced 2046d763cef2SBarry Smith 2047d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2048d763cef2SBarry Smith 2049d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith @*/ 20527087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2053d763cef2SBarry Smith { 2054dfbe8321SBarry Smith PetscErrorCode ierr; 2055089b2837SJed Brown SNES snes; 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith PetscFunctionBegin; 20580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2059d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2060089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2061089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2062d763cef2SBarry Smith PetscFunctionReturn(0); 2063d763cef2SBarry Smith } 2064d763cef2SBarry Smith 20654a2ae208SSatish Balay #undef __FUNCT__ 20664a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2067d763cef2SBarry Smith /*@C 2068d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2069d763cef2SBarry Smith TS options in the database. 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith Not Collective 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith Input Parameter: 2074d763cef2SBarry Smith . ts - The TS context 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith Output Parameter: 2077d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2080d763cef2SBarry Smith sufficient length to hold the prefix. 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith Level: intermediate 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2087d763cef2SBarry Smith @*/ 20887087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2089d763cef2SBarry Smith { 2090dfbe8321SBarry Smith PetscErrorCode ierr; 2091d763cef2SBarry Smith 2092d763cef2SBarry Smith PetscFunctionBegin; 20930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20944482741eSBarry Smith PetscValidPointer(prefix,2); 2095d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2096d763cef2SBarry Smith PetscFunctionReturn(0); 2097d763cef2SBarry Smith } 2098d763cef2SBarry Smith 20994a2ae208SSatish Balay #undef __FUNCT__ 21004a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2101d763cef2SBarry Smith /*@C 2102d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2103d763cef2SBarry Smith 2104d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2105d763cef2SBarry Smith 2106d763cef2SBarry Smith Input Parameter: 2107d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2108d763cef2SBarry Smith 2109d763cef2SBarry Smith Output Parameters: 2110d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2111d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2112089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2113d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2114d763cef2SBarry Smith 2115d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2116d763cef2SBarry Smith 2117d763cef2SBarry Smith Level: intermediate 2118d763cef2SBarry Smith 211926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2120d763cef2SBarry Smith 2121d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2122d763cef2SBarry Smith @*/ 2123089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2124d763cef2SBarry Smith { 2125089b2837SJed Brown PetscErrorCode ierr; 2126089b2837SJed Brown SNES snes; 2127089b2837SJed Brown 2128d763cef2SBarry Smith PetscFunctionBegin; 2129089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2130089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21313b5f76d0SSean Farley if (func) *func = ts->userops->rhsjacobian; 213226d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2133d763cef2SBarry Smith PetscFunctionReturn(0); 2134d763cef2SBarry Smith } 2135d763cef2SBarry Smith 21361713a123SBarry Smith #undef __FUNCT__ 21372eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 21382eca1d9cSJed Brown /*@C 21392eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 21402eca1d9cSJed Brown 21412eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 21422eca1d9cSJed Brown 21432eca1d9cSJed Brown Input Parameter: 21442eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 21452eca1d9cSJed Brown 21462eca1d9cSJed Brown Output Parameters: 21472eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 21482eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 21492eca1d9cSJed Brown . f - The function to compute the matrices 21502eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 21512eca1d9cSJed Brown 21522eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 21532eca1d9cSJed Brown 21542eca1d9cSJed Brown Level: advanced 21552eca1d9cSJed Brown 21562eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 21572eca1d9cSJed Brown 21582eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 21592eca1d9cSJed Brown @*/ 21607087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21612eca1d9cSJed Brown { 2162089b2837SJed Brown PetscErrorCode ierr; 2163089b2837SJed Brown SNES snes; 2164089b2837SJed Brown 21652eca1d9cSJed Brown PetscFunctionBegin; 2166089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2167089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21683b5f76d0SSean Farley if (f) *f = ts->userops->ijacobian; 21692eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21702eca1d9cSJed Brown PetscFunctionReturn(0); 21712eca1d9cSJed Brown } 21722eca1d9cSJed Brown 21732eca1d9cSJed Brown #undef __FUNCT__ 2174a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21751713a123SBarry Smith /*@C 2176a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21771713a123SBarry Smith VecView() for the solution at each timestep 21781713a123SBarry Smith 21791713a123SBarry Smith Collective on TS 21801713a123SBarry Smith 21811713a123SBarry Smith Input Parameters: 21821713a123SBarry Smith + ts - the TS context 21831713a123SBarry Smith . step - current time-step 2184142b95e3SSatish Balay . ptime - current time 21851713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21861713a123SBarry Smith 21871713a123SBarry Smith Level: intermediate 21881713a123SBarry Smith 21891713a123SBarry Smith .keywords: TS, vector, monitor, view 21901713a123SBarry Smith 2191a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21921713a123SBarry Smith @*/ 21937087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21941713a123SBarry Smith { 2195dfbe8321SBarry Smith PetscErrorCode ierr; 21961713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21971713a123SBarry Smith 21981713a123SBarry Smith PetscFunctionBegin; 2199a34d58ebSBarry Smith if (!dummy) { 22007adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 22011713a123SBarry Smith } 22021713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 22031713a123SBarry Smith PetscFunctionReturn(0); 22041713a123SBarry Smith } 22051713a123SBarry Smith 22061713a123SBarry Smith 22076c699258SBarry Smith #undef __FUNCT__ 22086c699258SBarry Smith #define __FUNCT__ "TSSetDM" 22096c699258SBarry Smith /*@ 22106c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 22116c699258SBarry Smith 22123f9fe445SBarry Smith Logically Collective on TS and DM 22136c699258SBarry Smith 22146c699258SBarry Smith Input Parameters: 22156c699258SBarry Smith + ts - the preconditioner context 22166c699258SBarry Smith - dm - the dm 22176c699258SBarry Smith 22186c699258SBarry Smith Level: intermediate 22196c699258SBarry Smith 22206c699258SBarry Smith 22216c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 22226c699258SBarry Smith @*/ 22237087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 22246c699258SBarry Smith { 22256c699258SBarry Smith PetscErrorCode ierr; 2226089b2837SJed Brown SNES snes; 22276c699258SBarry Smith 22286c699258SBarry Smith PetscFunctionBegin; 22290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 223070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 22316bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 22326c699258SBarry Smith ts->dm = dm; 2233089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2234089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 22356c699258SBarry Smith PetscFunctionReturn(0); 22366c699258SBarry Smith } 22376c699258SBarry Smith 22386c699258SBarry Smith #undef __FUNCT__ 22396c699258SBarry Smith #define __FUNCT__ "TSGetDM" 22406c699258SBarry Smith /*@ 22416c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 22426c699258SBarry Smith 22433f9fe445SBarry Smith Not Collective 22446c699258SBarry Smith 22456c699258SBarry Smith Input Parameter: 22466c699258SBarry Smith . ts - the preconditioner context 22476c699258SBarry Smith 22486c699258SBarry Smith Output Parameter: 22496c699258SBarry Smith . dm - the dm 22506c699258SBarry Smith 22516c699258SBarry Smith Level: intermediate 22526c699258SBarry Smith 22536c699258SBarry Smith 22546c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 22556c699258SBarry Smith @*/ 22567087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 22576c699258SBarry Smith { 22586c699258SBarry Smith PetscFunctionBegin; 22590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22606c699258SBarry Smith *dm = ts->dm; 22616c699258SBarry Smith PetscFunctionReturn(0); 22626c699258SBarry Smith } 22631713a123SBarry Smith 22640f5c6efeSJed Brown #undef __FUNCT__ 22650f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22660f5c6efeSJed Brown /*@ 22670f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22680f5c6efeSJed Brown 22693f9fe445SBarry Smith Logically Collective on SNES 22700f5c6efeSJed Brown 22710f5c6efeSJed Brown Input Parameter: 2272d42a1c89SJed Brown + snes - nonlinear solver 22730f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2274d42a1c89SJed Brown - ctx - user context, must be a TS 22750f5c6efeSJed Brown 22760f5c6efeSJed Brown Output Parameter: 22770f5c6efeSJed Brown . F - the nonlinear residual 22780f5c6efeSJed Brown 22790f5c6efeSJed Brown Notes: 22800f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22810f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22820f5c6efeSJed Brown 22830f5c6efeSJed Brown Level: advanced 22840f5c6efeSJed Brown 22850f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22860f5c6efeSJed Brown @*/ 22877087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22880f5c6efeSJed Brown { 22890f5c6efeSJed Brown TS ts = (TS)ctx; 22900f5c6efeSJed Brown PetscErrorCode ierr; 22910f5c6efeSJed Brown 22920f5c6efeSJed Brown PetscFunctionBegin; 22930f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22940f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22950f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22960f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22970f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22980f5c6efeSJed Brown PetscFunctionReturn(0); 22990f5c6efeSJed Brown } 23000f5c6efeSJed Brown 23010f5c6efeSJed Brown #undef __FUNCT__ 23020f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 23030f5c6efeSJed Brown /*@ 23040f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 23050f5c6efeSJed Brown 23060f5c6efeSJed Brown Collective on SNES 23070f5c6efeSJed Brown 23080f5c6efeSJed Brown Input Parameter: 23090f5c6efeSJed Brown + snes - nonlinear solver 23100f5c6efeSJed Brown . X - the current state at which to evaluate the residual 23110f5c6efeSJed Brown - ctx - user context, must be a TS 23120f5c6efeSJed Brown 23130f5c6efeSJed Brown Output Parameter: 23140f5c6efeSJed Brown + A - the Jacobian 23150f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 23160f5c6efeSJed Brown - flag - indicates any structure change in the matrix 23170f5c6efeSJed Brown 23180f5c6efeSJed Brown Notes: 23190f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 23200f5c6efeSJed Brown 23210f5c6efeSJed Brown Level: developer 23220f5c6efeSJed Brown 23230f5c6efeSJed Brown .seealso: SNESSetJacobian() 23240f5c6efeSJed Brown @*/ 23257087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 23260f5c6efeSJed Brown { 23270f5c6efeSJed Brown TS ts = (TS)ctx; 23280f5c6efeSJed Brown PetscErrorCode ierr; 23290f5c6efeSJed Brown 23300f5c6efeSJed Brown PetscFunctionBegin; 23310f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23320f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 23330f5c6efeSJed Brown PetscValidPointer(A,3); 23340f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 23350f5c6efeSJed Brown PetscValidPointer(B,4); 23360f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 23370f5c6efeSJed Brown PetscValidPointer(flag,5); 23380f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 23390f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 23400f5c6efeSJed Brown PetscFunctionReturn(0); 23410f5c6efeSJed Brown } 2342325fc9f4SBarry Smith 23430e4ef248SJed Brown #undef __FUNCT__ 23440e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 23450e4ef248SJed Brown /*@C 23460e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 23470e4ef248SJed Brown 23480e4ef248SJed Brown Collective on TS 23490e4ef248SJed Brown 23500e4ef248SJed Brown Input Arguments: 23510e4ef248SJed Brown + ts - time stepping context 23520e4ef248SJed Brown . t - time at which to evaluate 23530e4ef248SJed Brown . X - state at which to evaluate 23540e4ef248SJed Brown - ctx - context 23550e4ef248SJed Brown 23560e4ef248SJed Brown Output Arguments: 23570e4ef248SJed Brown . F - right hand side 23580e4ef248SJed Brown 23590e4ef248SJed Brown Level: intermediate 23600e4ef248SJed Brown 23610e4ef248SJed Brown Notes: 23620e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 23630e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 23640e4ef248SJed Brown 23650e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 23660e4ef248SJed Brown @*/ 23670e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 23680e4ef248SJed Brown { 23690e4ef248SJed Brown PetscErrorCode ierr; 23700e4ef248SJed Brown Mat Arhs,Brhs; 23710e4ef248SJed Brown MatStructure flg2; 23720e4ef248SJed Brown 23730e4ef248SJed Brown PetscFunctionBegin; 23740e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 23750e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 23760e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 23770e4ef248SJed Brown PetscFunctionReturn(0); 23780e4ef248SJed Brown } 23790e4ef248SJed Brown 23800e4ef248SJed Brown #undef __FUNCT__ 23810e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 23820e4ef248SJed Brown /*@C 23830e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 23840e4ef248SJed Brown 23850e4ef248SJed Brown Collective on TS 23860e4ef248SJed Brown 23870e4ef248SJed Brown Input Arguments: 23880e4ef248SJed Brown + ts - time stepping context 23890e4ef248SJed Brown . t - time at which to evaluate 23900e4ef248SJed Brown . X - state at which to evaluate 23910e4ef248SJed Brown - ctx - context 23920e4ef248SJed Brown 23930e4ef248SJed Brown Output Arguments: 23940e4ef248SJed Brown + A - pointer to operator 23950e4ef248SJed Brown . B - pointer to preconditioning matrix 23960e4ef248SJed Brown - flg - matrix structure flag 23970e4ef248SJed Brown 23980e4ef248SJed Brown Level: intermediate 23990e4ef248SJed Brown 24000e4ef248SJed Brown Notes: 24010e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 24020e4ef248SJed Brown 24030e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 24040e4ef248SJed Brown @*/ 24050e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 24060e4ef248SJed Brown { 24070e4ef248SJed Brown 24080e4ef248SJed Brown PetscFunctionBegin; 24090e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 24100e4ef248SJed Brown PetscFunctionReturn(0); 24110e4ef248SJed Brown } 24120e4ef248SJed Brown 24130026cea9SSean Farley #undef __FUNCT__ 24140026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 24150026cea9SSean Farley /*@C 24160026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 24170026cea9SSean Farley 24180026cea9SSean Farley Collective on TS 24190026cea9SSean Farley 24200026cea9SSean Farley Input Arguments: 24210026cea9SSean Farley + ts - time stepping context 24220026cea9SSean Farley . t - time at which to evaluate 24230026cea9SSean Farley . X - state at which to evaluate 24240026cea9SSean Farley . Xdot - time derivative of state vector 24250026cea9SSean Farley - ctx - context 24260026cea9SSean Farley 24270026cea9SSean Farley Output Arguments: 24280026cea9SSean Farley . F - left hand side 24290026cea9SSean Farley 24300026cea9SSean Farley Level: intermediate 24310026cea9SSean Farley 24320026cea9SSean Farley Notes: 24330026cea9SSean 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 24340026cea9SSean Farley user is required to write their own TSComputeIFunction. 24350026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 24360026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 24370026cea9SSean Farley 24380026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 24390026cea9SSean Farley @*/ 24400026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 24410026cea9SSean Farley { 24420026cea9SSean Farley PetscErrorCode ierr; 24430026cea9SSean Farley Mat A,B; 24440026cea9SSean Farley MatStructure flg2; 24450026cea9SSean Farley 24460026cea9SSean Farley PetscFunctionBegin; 24470026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 24480026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 24490026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 24500026cea9SSean Farley PetscFunctionReturn(0); 24510026cea9SSean Farley } 24520026cea9SSean Farley 24530026cea9SSean Farley #undef __FUNCT__ 24540026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 24550026cea9SSean Farley /*@C 24560026cea9SSean Farley TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 24570026cea9SSean Farley 24580026cea9SSean Farley Collective on TS 24590026cea9SSean Farley 24600026cea9SSean Farley Input Arguments: 24610026cea9SSean Farley + ts - time stepping context 24620026cea9SSean Farley . t - time at which to evaluate 24630026cea9SSean Farley . X - state at which to evaluate 24640026cea9SSean Farley . Xdot - time derivative of state vector 24650026cea9SSean Farley . shift - shift to apply 24660026cea9SSean Farley - ctx - context 24670026cea9SSean Farley 24680026cea9SSean Farley Output Arguments: 24690026cea9SSean Farley + A - pointer to operator 24700026cea9SSean Farley . B - pointer to preconditioning matrix 24710026cea9SSean Farley - flg - matrix structure flag 24720026cea9SSean Farley 24730026cea9SSean Farley Level: intermediate 24740026cea9SSean Farley 24750026cea9SSean Farley Notes: 24760026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 24770026cea9SSean Farley 24780026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 24790026cea9SSean Farley @*/ 24800026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 24810026cea9SSean Farley { 24820026cea9SSean Farley 24830026cea9SSean Farley PetscFunctionBegin; 24840026cea9SSean Farley *flg = SAME_PRECONDITIONER; 24850026cea9SSean Farley PetscFunctionReturn(0); 24860026cea9SSean Farley } 24870026cea9SSean Farley 24884af1b03aSJed Brown 24894af1b03aSJed Brown #undef __FUNCT__ 24904af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 24914af1b03aSJed Brown /*@ 24924af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 24934af1b03aSJed Brown 24944af1b03aSJed Brown Not Collective 24954af1b03aSJed Brown 24964af1b03aSJed Brown Input Parameter: 24974af1b03aSJed Brown . ts - the TS context 24984af1b03aSJed Brown 24994af1b03aSJed Brown Output Parameter: 25004af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 25014af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 25024af1b03aSJed Brown 25034af1b03aSJed Brown Level: intermediate 25044af1b03aSJed Brown 2505cd652676SJed Brown Notes: 2506cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 25074af1b03aSJed Brown 25084af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 25094af1b03aSJed Brown 25104af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 25114af1b03aSJed Brown @*/ 25124af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 25134af1b03aSJed Brown { 25144af1b03aSJed Brown PetscFunctionBegin; 25154af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25164af1b03aSJed Brown PetscValidPointer(reason,2); 25174af1b03aSJed Brown *reason = ts->reason; 25184af1b03aSJed Brown PetscFunctionReturn(0); 25194af1b03aSJed Brown } 25204af1b03aSJed Brown 2521325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2522c6db04a5SJed Brown #include <mex.h> 2523325fc9f4SBarry Smith 2524325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2525325fc9f4SBarry Smith 2526325fc9f4SBarry Smith #undef __FUNCT__ 2527325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2528325fc9f4SBarry Smith /* 2529325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2530325fc9f4SBarry Smith TSSetFunctionMatlab(). 2531325fc9f4SBarry Smith 2532325fc9f4SBarry Smith Collective on TS 2533325fc9f4SBarry Smith 2534325fc9f4SBarry Smith Input Parameters: 2535325fc9f4SBarry Smith + snes - the TS context 2536325fc9f4SBarry Smith - x - input vector 2537325fc9f4SBarry Smith 2538325fc9f4SBarry Smith Output Parameter: 2539325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2540325fc9f4SBarry Smith 2541325fc9f4SBarry Smith Notes: 2542325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2543325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2544325fc9f4SBarry Smith themselves. 2545325fc9f4SBarry Smith 2546325fc9f4SBarry Smith Level: developer 2547325fc9f4SBarry Smith 2548325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2549325fc9f4SBarry Smith 2550325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2551325fc9f4SBarry Smith */ 25527087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2553325fc9f4SBarry Smith { 2554325fc9f4SBarry Smith PetscErrorCode ierr; 2555325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2556325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2557325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2558325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2559325fc9f4SBarry Smith 2560325fc9f4SBarry Smith PetscFunctionBegin; 2561325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2562325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2563325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2564325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2565325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2566325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2567325fc9f4SBarry Smith 2568325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2569325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2570880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2571325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2572325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2573325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2574325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2575325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2576325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2577325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2578325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2579325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2580325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2581325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2582325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2583325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2584325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2585325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2586325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2587325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2588325fc9f4SBarry Smith PetscFunctionReturn(0); 2589325fc9f4SBarry Smith } 2590325fc9f4SBarry Smith 2591325fc9f4SBarry Smith 2592325fc9f4SBarry Smith #undef __FUNCT__ 2593325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2594325fc9f4SBarry Smith /* 2595325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2596325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2597e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2598325fc9f4SBarry Smith 2599325fc9f4SBarry Smith Logically Collective on TS 2600325fc9f4SBarry Smith 2601325fc9f4SBarry Smith Input Parameters: 2602325fc9f4SBarry Smith + ts - the TS context 2603325fc9f4SBarry Smith - func - function evaluation routine 2604325fc9f4SBarry Smith 2605325fc9f4SBarry Smith Calling sequence of func: 2606325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2607325fc9f4SBarry Smith 2608325fc9f4SBarry Smith Level: beginner 2609325fc9f4SBarry Smith 2610325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2611325fc9f4SBarry Smith 2612325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2613325fc9f4SBarry Smith */ 2614*cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 2615325fc9f4SBarry Smith { 2616325fc9f4SBarry Smith PetscErrorCode ierr; 2617325fc9f4SBarry Smith TSMatlabContext *sctx; 2618325fc9f4SBarry Smith 2619325fc9f4SBarry Smith PetscFunctionBegin; 2620325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2621325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2622325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2623325fc9f4SBarry Smith /* 2624325fc9f4SBarry Smith This should work, but it doesn't 2625325fc9f4SBarry Smith sctx->ctx = ctx; 2626325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2627325fc9f4SBarry Smith */ 2628325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2629*cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2630325fc9f4SBarry Smith PetscFunctionReturn(0); 2631325fc9f4SBarry Smith } 2632325fc9f4SBarry Smith 2633325fc9f4SBarry Smith #undef __FUNCT__ 2634325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2635325fc9f4SBarry Smith /* 2636325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2637325fc9f4SBarry Smith TSSetJacobianMatlab(). 2638325fc9f4SBarry Smith 2639325fc9f4SBarry Smith Collective on TS 2640325fc9f4SBarry Smith 2641325fc9f4SBarry Smith Input Parameters: 2642*cdcf91faSSean Farley + ts - the TS context 2643325fc9f4SBarry Smith . x - input vector 2644325fc9f4SBarry Smith . A, B - the matrices 2645325fc9f4SBarry Smith - ctx - user context 2646325fc9f4SBarry Smith 2647325fc9f4SBarry Smith Output Parameter: 2648325fc9f4SBarry Smith . flag - structure of the matrix 2649325fc9f4SBarry Smith 2650325fc9f4SBarry Smith Level: developer 2651325fc9f4SBarry Smith 2652325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2653325fc9f4SBarry Smith 2654325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2655325fc9f4SBarry Smith @*/ 2656*cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2657325fc9f4SBarry Smith { 2658325fc9f4SBarry Smith PetscErrorCode ierr; 2659325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2660325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2661325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2662325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2663325fc9f4SBarry Smith 2664325fc9f4SBarry Smith PetscFunctionBegin; 2665*cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2666325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2667325fc9f4SBarry Smith 2668325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2669325fc9f4SBarry Smith 2670*cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 2671325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2672325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2673325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2674325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2675325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2676325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2677325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2678325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2679325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2680325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2681325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2682325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2683325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2684325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2685325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2686325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2687325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2688325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2689325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2690325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2691325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2692325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2693325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2694325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2695325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2696325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2697325fc9f4SBarry Smith PetscFunctionReturn(0); 2698325fc9f4SBarry Smith } 2699325fc9f4SBarry Smith 2700325fc9f4SBarry Smith 2701325fc9f4SBarry Smith #undef __FUNCT__ 2702325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2703325fc9f4SBarry Smith /* 2704325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2705e3c5b3baSBarry 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 2706325fc9f4SBarry Smith 2707325fc9f4SBarry Smith Logically Collective on TS 2708325fc9f4SBarry Smith 2709325fc9f4SBarry Smith Input Parameters: 2710*cdcf91faSSean Farley + ts - the TS context 2711325fc9f4SBarry Smith . A,B - Jacobian matrices 2712325fc9f4SBarry Smith . func - function evaluation routine 2713325fc9f4SBarry Smith - ctx - user context 2714325fc9f4SBarry Smith 2715325fc9f4SBarry Smith Calling sequence of func: 2716*cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2717325fc9f4SBarry Smith 2718325fc9f4SBarry Smith 2719325fc9f4SBarry Smith Level: developer 2720325fc9f4SBarry Smith 2721325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2722325fc9f4SBarry Smith 2723325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2724325fc9f4SBarry Smith */ 2725*cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 2726325fc9f4SBarry Smith { 2727325fc9f4SBarry Smith PetscErrorCode ierr; 2728325fc9f4SBarry Smith TSMatlabContext *sctx; 2729325fc9f4SBarry Smith 2730325fc9f4SBarry Smith PetscFunctionBegin; 2731325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2732325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2733325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2734325fc9f4SBarry Smith /* 2735325fc9f4SBarry Smith This should work, but it doesn't 2736325fc9f4SBarry Smith sctx->ctx = ctx; 2737325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2738325fc9f4SBarry Smith */ 2739325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2740*cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2741325fc9f4SBarry Smith PetscFunctionReturn(0); 2742325fc9f4SBarry Smith } 2743325fc9f4SBarry Smith 2744b5b1a830SBarry Smith #undef __FUNCT__ 2745b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2746b5b1a830SBarry Smith /* 2747b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2748b5b1a830SBarry Smith 2749b5b1a830SBarry Smith Collective on TS 2750b5b1a830SBarry Smith 2751b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2752b5b1a830SBarry Smith @*/ 2753*cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 2754b5b1a830SBarry Smith { 2755b5b1a830SBarry Smith PetscErrorCode ierr; 2756b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2757a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2758b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2759b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2760b5b1a830SBarry Smith 2761b5b1a830SBarry Smith PetscFunctionBegin; 2762*cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2763b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2764b5b1a830SBarry Smith 2765*cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 2766b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2767b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2768b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2769b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2770b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2771b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2772b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2773b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2774b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2775b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2776b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2777b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2778b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2779b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2780b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2781b5b1a830SBarry Smith PetscFunctionReturn(0); 2782b5b1a830SBarry Smith } 2783b5b1a830SBarry Smith 2784b5b1a830SBarry Smith 2785b5b1a830SBarry Smith #undef __FUNCT__ 2786b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2787b5b1a830SBarry Smith /* 2788b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2789b5b1a830SBarry Smith 2790b5b1a830SBarry Smith Level: developer 2791b5b1a830SBarry Smith 2792b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2793b5b1a830SBarry Smith 2794b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2795b5b1a830SBarry Smith */ 2796*cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 2797b5b1a830SBarry Smith { 2798b5b1a830SBarry Smith PetscErrorCode ierr; 2799b5b1a830SBarry Smith TSMatlabContext *sctx; 2800b5b1a830SBarry Smith 2801b5b1a830SBarry Smith PetscFunctionBegin; 2802b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2803b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2804b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2805b5b1a830SBarry Smith /* 2806b5b1a830SBarry Smith This should work, but it doesn't 2807b5b1a830SBarry Smith sctx->ctx = ctx; 2808b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2809b5b1a830SBarry Smith */ 2810b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2811*cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2812b5b1a830SBarry Smith PetscFunctionReturn(0); 2813b5b1a830SBarry Smith } 2814b5b1a830SBarry Smith 2815325fc9f4SBarry Smith #endif 2816