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 { 73ace3abfcSBarry Smith PetscBool opt,flg; 74dfbe8321SBarry Smith PetscErrorCode ierr; 75649052a6SBarry Smith PetscViewer monviewer; 76eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 77089b2837SJed Brown SNES snes; 781c3436cfSJed Brown TSAdapt adapt; 79bdad233fSMatthew Knepley 80bdad233fSMatthew Knepley PetscFunctionBegin; 810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 823194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 83a43b19c4SJed Brown /* Handle TS type options */ 84a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 85bdad233fSMatthew Knepley 86bdad233fSMatthew Knepley /* Handle generic TS options */ 87bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 88bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 89d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 90d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&ts->time_step,PETSC_NULL);CHKERRQ(ierr); 91a43b19c4SJed Brown opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time; 92a43b19c4SJed Brown ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr); 93461e00b3SSean Farley if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);} 94193ac0bcSJed 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); 95193ac0bcSJed Brown ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr); 96193ac0bcSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 971c3436cfSJed Brown ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 981c3436cfSJed Brown ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 99bdad233fSMatthew Knepley 100bdad233fSMatthew Knepley /* Monitor options */ 101a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 102eabae89aSBarry Smith if (flg) { 103649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 104649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 105bdad233fSMatthew Knepley } 1065180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1075180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1085180491cSLisandro Dalcin 10990d69ab7SBarry Smith opt = PETSC_FALSE; 110acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 111a7cc72afSBarry Smith if (opt) { 112a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 113bdad233fSMatthew Knepley } 11490d69ab7SBarry Smith opt = PETSC_FALSE; 115acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 116a7cc72afSBarry Smith if (opt) { 1176083293cSBarry Smith void *ctx; 1186083293cSBarry Smith ierr = TSMonitorSolutionCreate(ts,PETSC_NULL,&ctx);CHKERRQ(ierr); 1196083293cSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr); 120bdad233fSMatthew Knepley } 121fb1732b5SBarry Smith opt = PETSC_FALSE; 122fb1732b5SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 123fb1732b5SBarry Smith if (flg) { 124fb1732b5SBarry Smith PetscViewer ctx; 125fb1732b5SBarry Smith if (monfilename[0]) { 126fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 127fb1732b5SBarry Smith } else { 128fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 129fb1732b5SBarry Smith } 130fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 131fb1732b5SBarry Smith } 132bdad233fSMatthew Knepley 1331c3436cfSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 1341c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 1351c3436cfSJed Brown 136d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 137d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 138d52bd9f3SBarry Smith 139bdad233fSMatthew Knepley /* Handle specific TS options */ 140abc0a331SBarry Smith if (ts->ops->setfromoptions) { 141bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 142bdad233fSMatthew Knepley } 1435d973c19SBarry Smith 1445d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1455d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 146bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 147bdad233fSMatthew Knepley PetscFunctionReturn(0); 148bdad233fSMatthew Knepley } 149bdad233fSMatthew Knepley 150bdad233fSMatthew Knepley #undef __FUNCT__ 151cdcf91faSSean Farley #undef __FUNCT__ 1524a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 153a7a1495cSBarry Smith /*@ 1548c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 155a7a1495cSBarry Smith set with TSSetRHSJacobian(). 156a7a1495cSBarry Smith 157a7a1495cSBarry Smith Collective on TS and Vec 158a7a1495cSBarry Smith 159a7a1495cSBarry Smith Input Parameters: 160316643e7SJed Brown + ts - the TS context 161a7a1495cSBarry Smith . t - current timestep 162a7a1495cSBarry Smith - x - input vector 163a7a1495cSBarry Smith 164a7a1495cSBarry Smith Output Parameters: 165a7a1495cSBarry Smith + A - Jacobian matrix 166a7a1495cSBarry Smith . B - optional preconditioning matrix 167a7a1495cSBarry Smith - flag - flag indicating matrix structure 168a7a1495cSBarry Smith 169a7a1495cSBarry Smith Notes: 170a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 171a7a1495cSBarry Smith is used internally within the nonlinear solvers. 172a7a1495cSBarry Smith 17394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 174a7a1495cSBarry Smith flag parameter. 175a7a1495cSBarry Smith 176a7a1495cSBarry Smith Level: developer 177a7a1495cSBarry Smith 178a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 179a7a1495cSBarry Smith 18094b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 181a7a1495cSBarry Smith @*/ 1827087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 183a7a1495cSBarry Smith { 184dfbe8321SBarry Smith PetscErrorCode ierr; 1850e4ef248SJed Brown PetscInt Xstate; 186a7a1495cSBarry Smith 187a7a1495cSBarry Smith PetscFunctionBegin; 1880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1890700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 190c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 1910e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 1920e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 1930e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 1940e4ef248SJed Brown PetscFunctionReturn(0); 195f8ede8e7SJed Brown } 196d90be118SSean Farley 1973b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 198d90be118SSean Farley 1993b5f76d0SSean Farley if (ts->userops->rhsjacobian) { 200d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 201a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 202a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 2033b5f76d0SSean Farley ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 204a7a1495cSBarry Smith PetscStackPop; 205d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 206a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2070700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2080700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 209ef66eb69SBarry Smith } else { 210214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 211214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 212214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 213ef66eb69SBarry Smith } 2140e4ef248SJed Brown ts->rhsjacobian.time = t; 2150e4ef248SJed Brown ts->rhsjacobian.X = X; 2160e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2170e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 218a7a1495cSBarry Smith PetscFunctionReturn(0); 219a7a1495cSBarry Smith } 220a7a1495cSBarry Smith 2214a2ae208SSatish Balay #undef __FUNCT__ 2224a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 223316643e7SJed Brown /*@ 224d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 225d763cef2SBarry Smith 226316643e7SJed Brown Collective on TS and Vec 227316643e7SJed Brown 228316643e7SJed Brown Input Parameters: 229316643e7SJed Brown + ts - the TS context 230316643e7SJed Brown . t - current time 231316643e7SJed Brown - x - state vector 232316643e7SJed Brown 233316643e7SJed Brown Output Parameter: 234316643e7SJed Brown . y - right hand side 235316643e7SJed Brown 236316643e7SJed Brown Note: 237316643e7SJed Brown Most users should not need to explicitly call this routine, as it 238316643e7SJed Brown is used internally within the nonlinear solvers. 239316643e7SJed Brown 240316643e7SJed Brown Level: developer 241316643e7SJed Brown 242316643e7SJed Brown .keywords: TS, compute 243316643e7SJed Brown 244316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 245316643e7SJed Brown @*/ 246dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 247d763cef2SBarry Smith { 248dfbe8321SBarry Smith PetscErrorCode ierr; 249d763cef2SBarry Smith 250d763cef2SBarry Smith PetscFunctionBegin; 2510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2520700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2530700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 254d763cef2SBarry Smith 2553b5f76d0SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 256d763cef2SBarry Smith 257d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 2583b5f76d0SSean Farley if (ts->userops->rhsfunction) { 259d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 2603b5f76d0SSean Farley ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 261d763cef2SBarry Smith PetscStackPop; 262214bc6a2SJed Brown } else { 263214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 264b2cd27e8SJed Brown } 26544a41b28SSean Farley 266d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 267d763cef2SBarry Smith PetscFunctionReturn(0); 268d763cef2SBarry Smith } 269d763cef2SBarry Smith 2704a2ae208SSatish Balay #undef __FUNCT__ 271214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 272214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 273214bc6a2SJed Brown { 2742dd45cf8SJed Brown Vec F; 275214bc6a2SJed Brown PetscErrorCode ierr; 276214bc6a2SJed Brown 277214bc6a2SJed Brown PetscFunctionBegin; 2780da9a4f0SJed Brown *Frhs = PETSC_NULL; 2792dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 280214bc6a2SJed Brown if (!ts->Frhs) { 2812dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 282214bc6a2SJed Brown } 283214bc6a2SJed Brown *Frhs = ts->Frhs; 284214bc6a2SJed Brown PetscFunctionReturn(0); 285214bc6a2SJed Brown } 286214bc6a2SJed Brown 287214bc6a2SJed Brown #undef __FUNCT__ 288214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 289214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 290214bc6a2SJed Brown { 291214bc6a2SJed Brown Mat A,B; 2922dd45cf8SJed Brown PetscErrorCode ierr; 293214bc6a2SJed Brown 294214bc6a2SJed Brown PetscFunctionBegin; 295214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 296214bc6a2SJed Brown if (Arhs) { 297214bc6a2SJed Brown if (!ts->Arhs) { 298214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 299214bc6a2SJed Brown } 300214bc6a2SJed Brown *Arhs = ts->Arhs; 301214bc6a2SJed Brown } 302214bc6a2SJed Brown if (Brhs) { 303214bc6a2SJed Brown if (!ts->Brhs) { 304214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 305214bc6a2SJed Brown } 306214bc6a2SJed Brown *Brhs = ts->Brhs; 307214bc6a2SJed Brown } 308214bc6a2SJed Brown PetscFunctionReturn(0); 309214bc6a2SJed Brown } 310214bc6a2SJed Brown 311214bc6a2SJed Brown #undef __FUNCT__ 312316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 313316643e7SJed Brown /*@ 314316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 315316643e7SJed Brown 316316643e7SJed Brown Collective on TS and Vec 317316643e7SJed Brown 318316643e7SJed Brown Input Parameters: 319316643e7SJed Brown + ts - the TS context 320316643e7SJed Brown . t - current time 321316643e7SJed Brown . X - state vector 322214bc6a2SJed Brown . Xdot - time derivative of state vector 323214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 324316643e7SJed Brown 325316643e7SJed Brown Output Parameter: 326316643e7SJed Brown . Y - right hand side 327316643e7SJed Brown 328316643e7SJed Brown Note: 329316643e7SJed Brown Most users should not need to explicitly call this routine, as it 330316643e7SJed Brown is used internally within the nonlinear solvers. 331316643e7SJed Brown 332316643e7SJed Brown If the user did did not write their equations in implicit form, this 333316643e7SJed Brown function recasts them in implicit form. 334316643e7SJed Brown 335316643e7SJed Brown Level: developer 336316643e7SJed Brown 337316643e7SJed Brown .keywords: TS, compute 338316643e7SJed Brown 339316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 340316643e7SJed Brown @*/ 341214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 342316643e7SJed Brown { 343316643e7SJed Brown PetscErrorCode ierr; 344316643e7SJed Brown 345316643e7SJed Brown PetscFunctionBegin; 3460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3470700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3480700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3490700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 350316643e7SJed Brown 351d90be118SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 352d90be118SSean Farley 353316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 3543b5f76d0SSean Farley if (ts->userops->ifunction) { 355316643e7SJed Brown PetscStackPush("TS user implicit function"); 3563b5f76d0SSean Farley ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 357316643e7SJed Brown PetscStackPop; 358214bc6a2SJed Brown } 359214bc6a2SJed Brown if (imex) { 3603b5f76d0SSean Farley if (!ts->userops->ifunction) { 3612dd45cf8SJed Brown ierr = VecCopy(Xdot,Y);CHKERRQ(ierr); 3622dd45cf8SJed Brown } 3632dd45cf8SJed Brown } else if (ts->userops->rhsfunction) { 3642dd45cf8SJed Brown if (ts->userops->ifunction) { 365214bc6a2SJed Brown Vec Frhs; 366214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 367214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 368214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 3692dd45cf8SJed Brown } else { 3702dd45cf8SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 3712dd45cf8SJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 372316643e7SJed Brown } 3734a6899ffSJed Brown } 374316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 375316643e7SJed Brown PetscFunctionReturn(0); 376316643e7SJed Brown } 377316643e7SJed Brown 378316643e7SJed Brown #undef __FUNCT__ 379316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 380316643e7SJed Brown /*@ 381316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 382316643e7SJed Brown 383316643e7SJed Brown Collective on TS and Vec 384316643e7SJed Brown 385316643e7SJed Brown Input 386316643e7SJed Brown Input Parameters: 387316643e7SJed Brown + ts - the TS context 388316643e7SJed Brown . t - current timestep 389316643e7SJed Brown . X - state vector 390316643e7SJed Brown . Xdot - time derivative of state vector 391214bc6a2SJed Brown . shift - shift to apply, see note below 392214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 393316643e7SJed Brown 394316643e7SJed Brown Output Parameters: 395316643e7SJed Brown + A - Jacobian matrix 396316643e7SJed Brown . B - optional preconditioning matrix 397316643e7SJed Brown - flag - flag indicating matrix structure 398316643e7SJed Brown 399316643e7SJed Brown Notes: 400316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 401316643e7SJed Brown 402316643e7SJed Brown dF/dX + shift*dF/dXdot 403316643e7SJed Brown 404316643e7SJed Brown Most users should not need to explicitly call this routine, as it 405316643e7SJed Brown is used internally within the nonlinear solvers. 406316643e7SJed Brown 407316643e7SJed Brown Level: developer 408316643e7SJed Brown 409316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 410316643e7SJed Brown 411316643e7SJed Brown .seealso: TSSetIJacobian() 41263495f91SJed Brown @*/ 413214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 414316643e7SJed Brown { 4150026cea9SSean Farley PetscInt Xstate, Xdotstate; 416316643e7SJed Brown PetscErrorCode ierr; 417316643e7SJed Brown 418316643e7SJed Brown PetscFunctionBegin; 4190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4200700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4210700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 422316643e7SJed Brown PetscValidPointer(A,6); 4230700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 424316643e7SJed Brown PetscValidPointer(B,7); 4250700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 426316643e7SJed Brown PetscValidPointer(flg,8); 4270026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 4280026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 4290026cea9SSean 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))) { 4300026cea9SSean Farley *flg = ts->ijacobian.mstructure; 4310026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4320026cea9SSean Farley PetscFunctionReturn(0); 4330026cea9SSean Farley } 434316643e7SJed Brown 4353b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 436316643e7SJed Brown 4374e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 438316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 4393b5f76d0SSean Farley if (ts->userops->ijacobian) { 4402dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 441316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 4423b5f76d0SSean Farley ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 443316643e7SJed Brown PetscStackPop; 444214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 445214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 446214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 4474a6899ffSJed Brown } 448214bc6a2SJed Brown if (imex) { 4493b5f76d0SSean Farley if (!ts->userops->ijacobian) { /* system was written as Xdot = F(t,X) */ 450214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 4512dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 452214bc6a2SJed Brown if (*A != *B) { 453214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 454214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 455214bc6a2SJed Brown } 456214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 457214bc6a2SJed Brown } 458214bc6a2SJed Brown } else { 4593b5f76d0SSean Farley if (!ts->userops->ijacobian) { 460214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 461214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 462214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 463316643e7SJed Brown if (*A != *B) { 464316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 465316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 466316643e7SJed Brown } 4673b5f76d0SSean Farley } else if (ts->userops->rhsjacobian) { 468214bc6a2SJed Brown Mat Arhs,Brhs; 469214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 470214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 471214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 472214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 473214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 474214bc6a2SJed Brown if (*A != *B) { 475214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 476214bc6a2SJed Brown } 477214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 478214bc6a2SJed Brown } 479316643e7SJed Brown } 4800026cea9SSean Farley 4810026cea9SSean Farley ts->ijacobian.time = t; 4820026cea9SSean Farley ts->ijacobian.X = X; 4830026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 4840026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 4850026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 4860026cea9SSean Farley ts->ijacobian.shift = shift; 4870026cea9SSean Farley ts->ijacobian.imex = imex; 4880026cea9SSean Farley ts->ijacobian.mstructure = *flg; 489316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 490316643e7SJed Brown PetscFunctionReturn(0); 491316643e7SJed Brown } 492316643e7SJed Brown 493316643e7SJed Brown #undef __FUNCT__ 4944a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 495d763cef2SBarry Smith /*@C 496d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 497d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 498d763cef2SBarry Smith 4993f9fe445SBarry Smith Logically Collective on TS 500d763cef2SBarry Smith 501d763cef2SBarry Smith Input Parameters: 502d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 503ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 504d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 505d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 506d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 507d763cef2SBarry Smith 508d763cef2SBarry Smith Calling sequence of func: 50987828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 510d763cef2SBarry Smith 511d763cef2SBarry Smith + t - current timestep 512d763cef2SBarry Smith . u - input vector 513d763cef2SBarry Smith . F - function vector 514d763cef2SBarry Smith - ctx - [optional] user-defined function context 515d763cef2SBarry Smith 516d763cef2SBarry Smith Important: 51776f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 518d763cef2SBarry Smith 519d763cef2SBarry Smith Level: beginner 520d763cef2SBarry Smith 521d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 522d763cef2SBarry Smith 52376f2fa84SHong Zhang .seealso: TSSetMatrices() 524d763cef2SBarry Smith @*/ 525089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 526d763cef2SBarry Smith { 527089b2837SJed Brown PetscErrorCode ierr; 528089b2837SJed Brown SNES snes; 529d763cef2SBarry Smith 530089b2837SJed Brown PetscFunctionBegin; 5310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 532ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 5333b5f76d0SSean Farley if (f) ts->userops->rhsfunction = f; 5344e684422SJed Brown if (ctx) ts->funP = ctx; 535089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 536089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 537d763cef2SBarry Smith PetscFunctionReturn(0); 538d763cef2SBarry Smith } 539d763cef2SBarry Smith 5404a2ae208SSatish Balay #undef __FUNCT__ 5414a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 542d763cef2SBarry Smith /*@C 543d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 544d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 54576f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 546d763cef2SBarry Smith 5473f9fe445SBarry Smith Logically Collective on TS 548d763cef2SBarry Smith 549d763cef2SBarry Smith Input Parameters: 550d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 551d763cef2SBarry Smith . A - Jacobian matrix 552d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 553d763cef2SBarry Smith . f - the Jacobian evaluation routine 554d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 555d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 556d763cef2SBarry Smith 557d763cef2SBarry Smith Calling sequence of func: 55887828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 559d763cef2SBarry Smith 560d763cef2SBarry Smith + t - current timestep 561d763cef2SBarry Smith . u - input vector 562d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 563d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 564d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 56594b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 566d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 567d763cef2SBarry Smith 568d763cef2SBarry Smith Notes: 56994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 570d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 571d763cef2SBarry Smith 572d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 573d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 57456335db2SHong Zhang completely new matrix structure (not just different matrix elements) 575d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 576d763cef2SBarry Smith throughout the global iterations. 577d763cef2SBarry Smith 578d763cef2SBarry Smith Level: beginner 579d763cef2SBarry Smith 580d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 581d763cef2SBarry Smith 582d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 58376f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 584d763cef2SBarry Smith 585d763cef2SBarry Smith @*/ 586089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 587d763cef2SBarry Smith { 588277b19d0SLisandro Dalcin PetscErrorCode ierr; 589089b2837SJed Brown SNES snes; 590277b19d0SLisandro Dalcin 591d763cef2SBarry Smith PetscFunctionBegin; 5920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 593277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 594277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 595277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 596277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 597d763cef2SBarry Smith 5983b5f76d0SSean Farley if (f) ts->userops->rhsjacobian = f; 599277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 600089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 601dabe61deSJed Brown if (!ts->userops->ijacobian) { 602089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 6030e4ef248SJed Brown } 6040e4ef248SJed Brown if (A) { 6050e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 6060e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 6070e4ef248SJed Brown ts->Arhs = A; 6080e4ef248SJed Brown } 6090e4ef248SJed Brown if (B) { 6100e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 6110e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 6120e4ef248SJed Brown ts->Brhs = B; 6130e4ef248SJed Brown } 614d763cef2SBarry Smith PetscFunctionReturn(0); 615d763cef2SBarry Smith } 616d763cef2SBarry Smith 617316643e7SJed Brown 618316643e7SJed Brown #undef __FUNCT__ 619316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 620316643e7SJed Brown /*@C 621316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 622316643e7SJed Brown 6233f9fe445SBarry Smith Logically Collective on TS 624316643e7SJed Brown 625316643e7SJed Brown Input Parameters: 626316643e7SJed Brown + ts - the TS context obtained from TSCreate() 627ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 628316643e7SJed Brown . f - the function evaluation routine 629316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 630316643e7SJed Brown 631316643e7SJed Brown Calling sequence of f: 632316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 633316643e7SJed Brown 634316643e7SJed Brown + t - time at step/stage being solved 635316643e7SJed Brown . u - state vector 636316643e7SJed Brown . u_t - time derivative of state vector 637316643e7SJed Brown . F - function vector 638316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 639316643e7SJed Brown 640316643e7SJed Brown Important: 641316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 642316643e7SJed Brown 643316643e7SJed Brown Level: beginner 644316643e7SJed Brown 645316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 646316643e7SJed Brown 647316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 648316643e7SJed Brown @*/ 649089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 650316643e7SJed Brown { 651089b2837SJed Brown PetscErrorCode ierr; 652089b2837SJed Brown SNES snes; 653316643e7SJed Brown 654316643e7SJed Brown PetscFunctionBegin; 6550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 656ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 6573b5f76d0SSean Farley if (f) ts->userops->ifunction = f; 658089b2837SJed Brown if (ctx) ts->funP = ctx; 659089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 660089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 661089b2837SJed Brown PetscFunctionReturn(0); 662089b2837SJed Brown } 663089b2837SJed Brown 664089b2837SJed Brown #undef __FUNCT__ 665089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 666089b2837SJed Brown /*@C 667089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 668089b2837SJed Brown 669089b2837SJed Brown Not Collective 670089b2837SJed Brown 671089b2837SJed Brown Input Parameter: 672089b2837SJed Brown . ts - the TS context 673089b2837SJed Brown 674089b2837SJed Brown Output Parameter: 675089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 676089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 677089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 678089b2837SJed Brown 679089b2837SJed Brown Level: advanced 680089b2837SJed Brown 681089b2837SJed Brown .keywords: TS, nonlinear, get, function 682089b2837SJed Brown 683089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 684089b2837SJed Brown @*/ 685089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 686089b2837SJed Brown { 687089b2837SJed Brown PetscErrorCode ierr; 688089b2837SJed Brown SNES snes; 689089b2837SJed Brown 690089b2837SJed Brown PetscFunctionBegin; 691089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 692089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 693089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 6943b5f76d0SSean Farley if (func) *func = ts->userops->ifunction; 695089b2837SJed Brown if (ctx) *ctx = ts->funP; 696089b2837SJed Brown PetscFunctionReturn(0); 697089b2837SJed Brown } 698089b2837SJed Brown 699089b2837SJed Brown #undef __FUNCT__ 700089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 701089b2837SJed Brown /*@C 702089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 703089b2837SJed Brown 704089b2837SJed Brown Not Collective 705089b2837SJed Brown 706089b2837SJed Brown Input Parameter: 707089b2837SJed Brown . ts - the TS context 708089b2837SJed Brown 709089b2837SJed Brown Output Parameter: 710089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 711089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 712089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 713089b2837SJed Brown 714089b2837SJed Brown Level: advanced 715089b2837SJed Brown 716089b2837SJed Brown .keywords: TS, nonlinear, get, function 717089b2837SJed Brown 718089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 719089b2837SJed Brown @*/ 720089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 721089b2837SJed Brown { 722089b2837SJed Brown PetscErrorCode ierr; 723089b2837SJed Brown SNES snes; 724089b2837SJed Brown 725089b2837SJed Brown PetscFunctionBegin; 726089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 727089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 728089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7293b5f76d0SSean Farley if (func) *func = ts->userops->rhsfunction; 730089b2837SJed Brown if (ctx) *ctx = ts->funP; 731316643e7SJed Brown PetscFunctionReturn(0); 732316643e7SJed Brown } 733316643e7SJed Brown 734316643e7SJed Brown #undef __FUNCT__ 735316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 736316643e7SJed Brown /*@C 737a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 738a4f0a591SBarry Smith you provided with TSSetIFunction(). 739316643e7SJed Brown 7403f9fe445SBarry Smith Logically Collective on TS 741316643e7SJed Brown 742316643e7SJed Brown Input Parameters: 743316643e7SJed Brown + ts - the TS context obtained from TSCreate() 744316643e7SJed Brown . A - Jacobian matrix 745316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 746316643e7SJed Brown . f - the Jacobian evaluation routine 747316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 748316643e7SJed Brown 749316643e7SJed Brown Calling sequence of f: 7501b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 751316643e7SJed Brown 752316643e7SJed Brown + t - time at step/stage being solved 7531b4a444bSJed Brown . U - state vector 7541b4a444bSJed Brown . U_t - time derivative of state vector 755316643e7SJed Brown . a - shift 7561b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 757316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 758316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 759316643e7SJed Brown structure (same as flag in KSPSetOperators()) 760316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 761316643e7SJed Brown 762316643e7SJed Brown Notes: 763316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 764316643e7SJed Brown 765a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 766a4f0a591SBarry 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. 767a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 768a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 769a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 770a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 771a4f0a591SBarry Smith 772316643e7SJed Brown Level: beginner 773316643e7SJed Brown 774316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 775316643e7SJed Brown 776316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 777316643e7SJed Brown 778316643e7SJed Brown @*/ 7797087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 780316643e7SJed Brown { 781316643e7SJed Brown PetscErrorCode ierr; 782089b2837SJed Brown SNES snes; 783316643e7SJed Brown 784316643e7SJed Brown PetscFunctionBegin; 7850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7860700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 7870700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 788316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 789316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 7903b5f76d0SSean Farley if (f) ts->userops->ijacobian = f; 791316643e7SJed Brown if (ctx) ts->jacP = ctx; 792089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 793089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 794316643e7SJed Brown PetscFunctionReturn(0); 795316643e7SJed Brown } 796316643e7SJed Brown 7974a2ae208SSatish Balay #undef __FUNCT__ 7984a2ae208SSatish Balay #define __FUNCT__ "TSView" 7997e2c5f70SBarry Smith /*@C 800d763cef2SBarry Smith TSView - Prints the TS data structure. 801d763cef2SBarry Smith 8024c49b128SBarry Smith Collective on TS 803d763cef2SBarry Smith 804d763cef2SBarry Smith Input Parameters: 805d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 806d763cef2SBarry Smith - viewer - visualization context 807d763cef2SBarry Smith 808d763cef2SBarry Smith Options Database Key: 809d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 810d763cef2SBarry Smith 811d763cef2SBarry Smith Notes: 812d763cef2SBarry Smith The available visualization contexts include 813b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 814b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 815d763cef2SBarry Smith output where only the first processor opens 816d763cef2SBarry Smith the file. All other processors send their 817d763cef2SBarry Smith data to the first processor to print. 818d763cef2SBarry Smith 819d763cef2SBarry Smith The user can open an alternative visualization context with 820b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 821d763cef2SBarry Smith 822d763cef2SBarry Smith Level: beginner 823d763cef2SBarry Smith 824d763cef2SBarry Smith .keywords: TS, timestep, view 825d763cef2SBarry Smith 826b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 827d763cef2SBarry Smith @*/ 8287087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 829d763cef2SBarry Smith { 830dfbe8321SBarry Smith PetscErrorCode ierr; 831a313700dSBarry Smith const TSType type; 832089b2837SJed Brown PetscBool iascii,isstring,isundials; 833d763cef2SBarry Smith 834d763cef2SBarry Smith PetscFunctionBegin; 8350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8363050cee2SBarry Smith if (!viewer) { 8377adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8383050cee2SBarry Smith } 8390700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 840c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 841fd16b177SBarry Smith 8422692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8432692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 84432077d6dSBarry Smith if (iascii) { 845317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 84677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 847a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 848d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 84977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 850c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 851d763cef2SBarry Smith } 85277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 853193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 854d52bd9f3SBarry Smith if (ts->ops->view) { 855d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 856d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 857d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 858d52bd9f3SBarry Smith } 8590f5bd95cSBarry Smith } else if (isstring) { 860a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 861b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 862d763cef2SBarry Smith } 863b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 864089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 865b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 866d763cef2SBarry Smith PetscFunctionReturn(0); 867d763cef2SBarry Smith } 868d763cef2SBarry Smith 869d763cef2SBarry Smith 8704a2ae208SSatish Balay #undef __FUNCT__ 8714a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 872b07ff414SBarry Smith /*@ 873d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 874d763cef2SBarry Smith the timesteppers. 875d763cef2SBarry Smith 8763f9fe445SBarry Smith Logically Collective on TS 877d763cef2SBarry Smith 878d763cef2SBarry Smith Input Parameters: 879d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 880d763cef2SBarry Smith - usrP - optional user context 881d763cef2SBarry Smith 882d763cef2SBarry Smith Level: intermediate 883d763cef2SBarry Smith 884d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 885d763cef2SBarry Smith 886d763cef2SBarry Smith .seealso: TSGetApplicationContext() 887d763cef2SBarry Smith @*/ 8887087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 889d763cef2SBarry Smith { 890d763cef2SBarry Smith PetscFunctionBegin; 8910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 892d763cef2SBarry Smith ts->user = usrP; 893d763cef2SBarry Smith PetscFunctionReturn(0); 894d763cef2SBarry Smith } 895d763cef2SBarry Smith 8964a2ae208SSatish Balay #undef __FUNCT__ 8974a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 898b07ff414SBarry Smith /*@ 899d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 900d763cef2SBarry Smith timestepper. 901d763cef2SBarry Smith 902d763cef2SBarry Smith Not Collective 903d763cef2SBarry Smith 904d763cef2SBarry Smith Input Parameter: 905d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 906d763cef2SBarry Smith 907d763cef2SBarry Smith Output Parameter: 908d763cef2SBarry Smith . usrP - user context 909d763cef2SBarry Smith 910d763cef2SBarry Smith Level: intermediate 911d763cef2SBarry Smith 912d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 913d763cef2SBarry Smith 914d763cef2SBarry Smith .seealso: TSSetApplicationContext() 915d763cef2SBarry Smith @*/ 916e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 917d763cef2SBarry Smith { 918d763cef2SBarry Smith PetscFunctionBegin; 9190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 920e71120c6SJed Brown *(void**)usrP = ts->user; 921d763cef2SBarry Smith PetscFunctionReturn(0); 922d763cef2SBarry Smith } 923d763cef2SBarry Smith 9244a2ae208SSatish Balay #undef __FUNCT__ 9254a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 926d763cef2SBarry Smith /*@ 927d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 928d763cef2SBarry Smith 929d763cef2SBarry Smith Not Collective 930d763cef2SBarry Smith 931d763cef2SBarry Smith Input Parameter: 932d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 933d763cef2SBarry Smith 934d763cef2SBarry Smith Output Parameter: 935d763cef2SBarry Smith . iter - number steps so far 936d763cef2SBarry Smith 937d763cef2SBarry Smith Level: intermediate 938d763cef2SBarry Smith 939d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 940d763cef2SBarry Smith @*/ 9417087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 942d763cef2SBarry Smith { 943d763cef2SBarry Smith PetscFunctionBegin; 9440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9454482741eSBarry Smith PetscValidIntPointer(iter,2); 946d763cef2SBarry Smith *iter = ts->steps; 947d763cef2SBarry Smith PetscFunctionReturn(0); 948d763cef2SBarry Smith } 949d763cef2SBarry Smith 9504a2ae208SSatish Balay #undef __FUNCT__ 9514a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 952d763cef2SBarry Smith /*@ 953d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 954d763cef2SBarry Smith as well as the initial time. 955d763cef2SBarry Smith 9563f9fe445SBarry Smith Logically Collective on TS 957d763cef2SBarry Smith 958d763cef2SBarry Smith Input Parameters: 959d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 960d763cef2SBarry Smith . initial_time - the initial time 961d763cef2SBarry Smith - time_step - the size of the timestep 962d763cef2SBarry Smith 963d763cef2SBarry Smith Level: intermediate 964d763cef2SBarry Smith 965d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 966d763cef2SBarry Smith 967d763cef2SBarry Smith .keywords: TS, set, initial, timestep 968d763cef2SBarry Smith @*/ 9697087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 970d763cef2SBarry Smith { 971e144a568SJed Brown PetscErrorCode ierr; 972e144a568SJed Brown 973d763cef2SBarry Smith PetscFunctionBegin; 9740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 975e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 976d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 977d763cef2SBarry Smith PetscFunctionReturn(0); 978d763cef2SBarry Smith } 979d763cef2SBarry Smith 9804a2ae208SSatish Balay #undef __FUNCT__ 9814a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 982d763cef2SBarry Smith /*@ 983d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 984d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 985d763cef2SBarry Smith 9863f9fe445SBarry Smith Logically Collective on TS 987d763cef2SBarry Smith 988d763cef2SBarry Smith Input Parameters: 989d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 990d763cef2SBarry Smith - time_step - the size of the timestep 991d763cef2SBarry Smith 992d763cef2SBarry Smith Level: intermediate 993d763cef2SBarry Smith 994d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 995d763cef2SBarry Smith 996d763cef2SBarry Smith .keywords: TS, set, timestep 997d763cef2SBarry Smith @*/ 9987087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 999d763cef2SBarry Smith { 1000d763cef2SBarry Smith PetscFunctionBegin; 10010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1002c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1003d763cef2SBarry Smith ts->time_step = time_step; 1004d763cef2SBarry Smith PetscFunctionReturn(0); 1005d763cef2SBarry Smith } 1006d763cef2SBarry Smith 10074a2ae208SSatish Balay #undef __FUNCT__ 1008a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1009a43b19c4SJed Brown /*@ 1010a43b19c4SJed Brown TSSetExactFinalTime - Determines whether to interpolate solution to the 1011a43b19c4SJed Brown exact final time requested by the user or just returns it at the final time 1012a43b19c4SJed Brown it computed. 1013a43b19c4SJed Brown 1014a43b19c4SJed Brown Logically Collective on TS 1015a43b19c4SJed Brown 1016a43b19c4SJed Brown Input Parameter: 1017a43b19c4SJed Brown + ts - the time-step context 1018a43b19c4SJed Brown - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1019a43b19c4SJed Brown 1020a43b19c4SJed Brown Level: beginner 1021a43b19c4SJed Brown 1022a43b19c4SJed Brown .seealso: TSSetDuration() 1023a43b19c4SJed Brown @*/ 1024a43b19c4SJed Brown PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1025a43b19c4SJed Brown { 1026a43b19c4SJed Brown 1027a43b19c4SJed Brown PetscFunctionBegin; 1028a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1029d8cd7023SBarry Smith PetscValidLogicalCollectiveBool(ts,flg,2); 1030a43b19c4SJed Brown ts->exact_final_time = flg; 1031a43b19c4SJed Brown PetscFunctionReturn(0); 1032a43b19c4SJed Brown } 1033a43b19c4SJed Brown 1034a43b19c4SJed Brown #undef __FUNCT__ 10354a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1036d763cef2SBarry Smith /*@ 1037d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1038d763cef2SBarry Smith 1039d763cef2SBarry Smith Not Collective 1040d763cef2SBarry Smith 1041d763cef2SBarry Smith Input Parameter: 1042d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1043d763cef2SBarry Smith 1044d763cef2SBarry Smith Output Parameter: 1045d763cef2SBarry Smith . dt - the current timestep size 1046d763cef2SBarry Smith 1047d763cef2SBarry Smith Level: intermediate 1048d763cef2SBarry Smith 1049d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1050d763cef2SBarry Smith 1051d763cef2SBarry Smith .keywords: TS, get, timestep 1052d763cef2SBarry Smith @*/ 10537087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1054d763cef2SBarry Smith { 1055d763cef2SBarry Smith PetscFunctionBegin; 10560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10574482741eSBarry Smith PetscValidDoublePointer(dt,2); 1058d763cef2SBarry Smith *dt = ts->time_step; 1059d763cef2SBarry Smith PetscFunctionReturn(0); 1060d763cef2SBarry Smith } 1061d763cef2SBarry Smith 10624a2ae208SSatish Balay #undef __FUNCT__ 10634a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1064d8e5e3e6SSatish Balay /*@ 1065d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1066d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1067d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1068d763cef2SBarry Smith the solution at the next timestep has been calculated. 1069d763cef2SBarry Smith 1070d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1071d763cef2SBarry Smith 1072d763cef2SBarry Smith Input Parameter: 1073d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1074d763cef2SBarry Smith 1075d763cef2SBarry Smith Output Parameter: 1076d763cef2SBarry Smith . v - the vector containing the solution 1077d763cef2SBarry Smith 1078d763cef2SBarry Smith Level: intermediate 1079d763cef2SBarry Smith 1080d763cef2SBarry Smith .seealso: TSGetTimeStep() 1081d763cef2SBarry Smith 1082d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1083d763cef2SBarry Smith @*/ 10847087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1085d763cef2SBarry Smith { 1086d763cef2SBarry Smith PetscFunctionBegin; 10870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10884482741eSBarry Smith PetscValidPointer(v,2); 10898737fe31SLisandro Dalcin *v = ts->vec_sol; 1090d763cef2SBarry Smith PetscFunctionReturn(0); 1091d763cef2SBarry Smith } 1092d763cef2SBarry Smith 1093bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 10944a2ae208SSatish Balay #undef __FUNCT__ 1095bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1096d8e5e3e6SSatish Balay /*@ 1097bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1098d763cef2SBarry Smith 1099bdad233fSMatthew Knepley Not collective 1100d763cef2SBarry Smith 1101bdad233fSMatthew Knepley Input Parameters: 1102bdad233fSMatthew Knepley + ts - The TS 1103bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1104d763cef2SBarry Smith .vb 1105d763cef2SBarry Smith U_t = A U 1106d763cef2SBarry Smith U_t = A(t) U 1107d763cef2SBarry Smith U_t = F(t,U) 1108d763cef2SBarry Smith .ve 1109d763cef2SBarry Smith 1110d763cef2SBarry Smith Level: beginner 1111d763cef2SBarry Smith 1112bdad233fSMatthew Knepley .keywords: TS, problem type 1113bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1114d763cef2SBarry Smith @*/ 11157087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1116a7cc72afSBarry Smith { 11179e2a6581SJed Brown PetscErrorCode ierr; 11189e2a6581SJed Brown 1119d763cef2SBarry Smith PetscFunctionBegin; 11200700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1121bdad233fSMatthew Knepley ts->problem_type = type; 11229e2a6581SJed Brown if (type == TS_LINEAR) { 11239e2a6581SJed Brown SNES snes; 11249e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11259e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 11269e2a6581SJed Brown } 1127d763cef2SBarry Smith PetscFunctionReturn(0); 1128d763cef2SBarry Smith } 1129d763cef2SBarry Smith 1130bdad233fSMatthew Knepley #undef __FUNCT__ 1131bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1132bdad233fSMatthew Knepley /*@C 1133bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1134bdad233fSMatthew Knepley 1135bdad233fSMatthew Knepley Not collective 1136bdad233fSMatthew Knepley 1137bdad233fSMatthew Knepley Input Parameter: 1138bdad233fSMatthew Knepley . ts - The TS 1139bdad233fSMatthew Knepley 1140bdad233fSMatthew Knepley Output Parameter: 1141bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1142bdad233fSMatthew Knepley .vb 1143089b2837SJed Brown M U_t = A U 1144089b2837SJed Brown M(t) U_t = A(t) U 1145bdad233fSMatthew Knepley U_t = F(t,U) 1146bdad233fSMatthew Knepley .ve 1147bdad233fSMatthew Knepley 1148bdad233fSMatthew Knepley Level: beginner 1149bdad233fSMatthew Knepley 1150bdad233fSMatthew Knepley .keywords: TS, problem type 1151bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1152bdad233fSMatthew Knepley @*/ 11537087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1154a7cc72afSBarry Smith { 1155bdad233fSMatthew Knepley PetscFunctionBegin; 11560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11574482741eSBarry Smith PetscValidIntPointer(type,2); 1158bdad233fSMatthew Knepley *type = ts->problem_type; 1159bdad233fSMatthew Knepley PetscFunctionReturn(0); 1160bdad233fSMatthew Knepley } 1161d763cef2SBarry Smith 11624a2ae208SSatish Balay #undef __FUNCT__ 11634a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1164d763cef2SBarry Smith /*@ 1165d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1166d763cef2SBarry Smith of a timestepper. 1167d763cef2SBarry Smith 1168d763cef2SBarry Smith Collective on TS 1169d763cef2SBarry Smith 1170d763cef2SBarry Smith Input Parameter: 1171d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1172d763cef2SBarry Smith 1173d763cef2SBarry Smith Notes: 1174d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1175d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1176d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1177d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1178d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1179d763cef2SBarry Smith 1180d763cef2SBarry Smith Level: advanced 1181d763cef2SBarry Smith 1182d763cef2SBarry Smith .keywords: TS, timestep, setup 1183d763cef2SBarry Smith 1184d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1185d763cef2SBarry Smith @*/ 11867087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1187d763cef2SBarry Smith { 1188dfbe8321SBarry Smith PetscErrorCode ierr; 1189d763cef2SBarry Smith 1190d763cef2SBarry Smith PetscFunctionBegin; 11910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1192277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1193277b19d0SLisandro Dalcin 11947adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 11959596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1196d763cef2SBarry Smith } 1197a43b19c4SJed Brown if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1198277b19d0SLisandro Dalcin 1199277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1200277b19d0SLisandro Dalcin 12011c3436cfSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 12021c3436cfSJed Brown 1203277b19d0SLisandro Dalcin if (ts->ops->setup) { 1204000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1205277b19d0SLisandro Dalcin } 1206277b19d0SLisandro Dalcin 1207277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1208277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1209277b19d0SLisandro Dalcin } 1210277b19d0SLisandro Dalcin 1211277b19d0SLisandro Dalcin #undef __FUNCT__ 1212277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1213277b19d0SLisandro Dalcin /*@ 1214277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1215277b19d0SLisandro Dalcin 1216277b19d0SLisandro Dalcin Collective on TS 1217277b19d0SLisandro Dalcin 1218277b19d0SLisandro Dalcin Input Parameter: 1219277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1220277b19d0SLisandro Dalcin 1221277b19d0SLisandro Dalcin Level: beginner 1222277b19d0SLisandro Dalcin 1223277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1224277b19d0SLisandro Dalcin 1225277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1226277b19d0SLisandro Dalcin @*/ 1227277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1228277b19d0SLisandro Dalcin { 1229277b19d0SLisandro Dalcin PetscErrorCode ierr; 1230277b19d0SLisandro Dalcin 1231277b19d0SLisandro Dalcin PetscFunctionBegin; 1232277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1233277b19d0SLisandro Dalcin if (ts->ops->reset) { 1234277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1235277b19d0SLisandro Dalcin } 1236277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12374e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 12384e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1239214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 12406bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 124138637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1242277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1243d763cef2SBarry Smith PetscFunctionReturn(0); 1244d763cef2SBarry Smith } 1245d763cef2SBarry Smith 12464a2ae208SSatish Balay #undef __FUNCT__ 12474a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1248d8e5e3e6SSatish Balay /*@ 1249d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1250d763cef2SBarry Smith with TSCreate(). 1251d763cef2SBarry Smith 1252d763cef2SBarry Smith Collective on TS 1253d763cef2SBarry Smith 1254d763cef2SBarry Smith Input Parameter: 1255d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1256d763cef2SBarry Smith 1257d763cef2SBarry Smith Level: beginner 1258d763cef2SBarry Smith 1259d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1260d763cef2SBarry Smith 1261d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1262d763cef2SBarry Smith @*/ 12636bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1264d763cef2SBarry Smith { 12656849ba73SBarry Smith PetscErrorCode ierr; 1266d763cef2SBarry Smith 1267d763cef2SBarry Smith PetscFunctionBegin; 12686bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12696bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12706bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1271d763cef2SBarry Smith 12726bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1273277b19d0SLisandro Dalcin 1274be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12756bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 12766bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 12776d4c513bSLisandro Dalcin 127884df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 12796bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 12806bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 12816bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 12826d4c513bSLisandro Dalcin 12833b5f76d0SSean Farley ierr = PetscFree((*ts)->userops); 12843b5f76d0SSean Farley 1285a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1286d763cef2SBarry Smith PetscFunctionReturn(0); 1287d763cef2SBarry Smith } 1288d763cef2SBarry Smith 12894a2ae208SSatish Balay #undef __FUNCT__ 12904a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1291d8e5e3e6SSatish Balay /*@ 1292d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1293d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1294d763cef2SBarry Smith 1295d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1296d763cef2SBarry Smith 1297d763cef2SBarry Smith Input Parameter: 1298d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1299d763cef2SBarry Smith 1300d763cef2SBarry Smith Output Parameter: 1301d763cef2SBarry Smith . snes - the nonlinear solver context 1302d763cef2SBarry Smith 1303d763cef2SBarry Smith Notes: 1304d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1305d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 130694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1307d763cef2SBarry Smith 1308d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1309d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1310d763cef2SBarry Smith 1311d763cef2SBarry Smith Level: beginner 1312d763cef2SBarry Smith 1313d763cef2SBarry Smith .keywords: timestep, get, SNES 1314d763cef2SBarry Smith @*/ 13157087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1316d763cef2SBarry Smith { 1317d372ba47SLisandro Dalcin PetscErrorCode ierr; 1318d372ba47SLisandro Dalcin 1319d763cef2SBarry Smith PetscFunctionBegin; 13200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13214482741eSBarry Smith PetscValidPointer(snes,2); 1322d372ba47SLisandro Dalcin if (!ts->snes) { 1323d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1324d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1325d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 13269e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 13279e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 13289e2a6581SJed Brown } 1329d372ba47SLisandro Dalcin } 1330d763cef2SBarry Smith *snes = ts->snes; 1331d763cef2SBarry Smith PetscFunctionReturn(0); 1332d763cef2SBarry Smith } 1333d763cef2SBarry Smith 13344a2ae208SSatish Balay #undef __FUNCT__ 133594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1336d8e5e3e6SSatish Balay /*@ 133794b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1338d763cef2SBarry Smith a TS (timestepper) context. 1339d763cef2SBarry Smith 134094b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1341d763cef2SBarry Smith 1342d763cef2SBarry Smith Input Parameter: 1343d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1344d763cef2SBarry Smith 1345d763cef2SBarry Smith Output Parameter: 134694b7f48cSBarry Smith . ksp - the nonlinear solver context 1347d763cef2SBarry Smith 1348d763cef2SBarry Smith Notes: 134994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1350d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1351d763cef2SBarry Smith KSP and PC contexts as well. 1352d763cef2SBarry Smith 135394b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 135494b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1355d763cef2SBarry Smith 1356d763cef2SBarry Smith Level: beginner 1357d763cef2SBarry Smith 135894b7f48cSBarry Smith .keywords: timestep, get, KSP 1359d763cef2SBarry Smith @*/ 13607087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1361d763cef2SBarry Smith { 1362d372ba47SLisandro Dalcin PetscErrorCode ierr; 1363089b2837SJed Brown SNES snes; 1364d372ba47SLisandro Dalcin 1365d763cef2SBarry Smith PetscFunctionBegin; 13660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13674482741eSBarry Smith PetscValidPointer(ksp,2); 136817186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1369e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1370089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1371089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1372d763cef2SBarry Smith PetscFunctionReturn(0); 1373d763cef2SBarry Smith } 1374d763cef2SBarry Smith 1375d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1376d763cef2SBarry Smith 13774a2ae208SSatish Balay #undef __FUNCT__ 1378adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1379adb62b0dSMatthew Knepley /*@ 1380adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1381adb62b0dSMatthew Knepley maximum time for iteration. 1382adb62b0dSMatthew Knepley 13833f9fe445SBarry Smith Not Collective 1384adb62b0dSMatthew Knepley 1385adb62b0dSMatthew Knepley Input Parameters: 1386adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1387adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1388adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1389adb62b0dSMatthew Knepley 1390adb62b0dSMatthew Knepley Level: intermediate 1391adb62b0dSMatthew Knepley 1392adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1393adb62b0dSMatthew Knepley @*/ 13947087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1395adb62b0dSMatthew Knepley { 1396adb62b0dSMatthew Knepley PetscFunctionBegin; 13970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1398abc0a331SBarry Smith if (maxsteps) { 13994482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1400adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1401adb62b0dSMatthew Knepley } 1402abc0a331SBarry Smith if (maxtime) { 14034482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1404adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1405adb62b0dSMatthew Knepley } 1406adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1407adb62b0dSMatthew Knepley } 1408adb62b0dSMatthew Knepley 1409adb62b0dSMatthew Knepley #undef __FUNCT__ 14104a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1411d763cef2SBarry Smith /*@ 1412d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1413d763cef2SBarry Smith maximum time for iteration. 1414d763cef2SBarry Smith 14153f9fe445SBarry Smith Logically Collective on TS 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith Input Parameters: 1418d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1419d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1420d763cef2SBarry Smith - maxtime - final time to iterate to 1421d763cef2SBarry Smith 1422d763cef2SBarry Smith Options Database Keys: 1423d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1424d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1425d763cef2SBarry Smith 1426d763cef2SBarry Smith Notes: 1427d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1428d763cef2SBarry Smith 1429d763cef2SBarry Smith Level: intermediate 1430d763cef2SBarry Smith 1431d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1432a43b19c4SJed Brown 1433a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1434d763cef2SBarry Smith @*/ 14357087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1436d763cef2SBarry Smith { 1437d763cef2SBarry Smith PetscFunctionBegin; 14380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1439c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1440c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 144139b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 144239b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1443d763cef2SBarry Smith PetscFunctionReturn(0); 1444d763cef2SBarry Smith } 1445d763cef2SBarry Smith 14464a2ae208SSatish Balay #undef __FUNCT__ 14474a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1448d763cef2SBarry Smith /*@ 1449d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1450d763cef2SBarry Smith for use by the TS routines. 1451d763cef2SBarry Smith 14523f9fe445SBarry Smith Logically Collective on TS and Vec 1453d763cef2SBarry Smith 1454d763cef2SBarry Smith Input Parameters: 1455d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1456d763cef2SBarry Smith - x - the solution vector 1457d763cef2SBarry Smith 1458d763cef2SBarry Smith Level: beginner 1459d763cef2SBarry Smith 1460d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1461d763cef2SBarry Smith @*/ 14627087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1463d763cef2SBarry Smith { 14648737fe31SLisandro Dalcin PetscErrorCode ierr; 14658737fe31SLisandro Dalcin 1466d763cef2SBarry Smith PetscFunctionBegin; 14670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14680700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14698737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14706bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14718737fe31SLisandro Dalcin ts->vec_sol = x; 1472d763cef2SBarry Smith PetscFunctionReturn(0); 1473d763cef2SBarry Smith } 1474d763cef2SBarry Smith 1475e74ef692SMatthew Knepley #undef __FUNCT__ 1476e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1477ac226902SBarry Smith /*@C 1478000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14793f2090d5SJed Brown called once at the beginning of each time step. 1480000e7ae3SMatthew Knepley 14813f9fe445SBarry Smith Logically Collective on TS 1482000e7ae3SMatthew Knepley 1483000e7ae3SMatthew Knepley Input Parameters: 1484000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1485000e7ae3SMatthew Knepley - func - The function 1486000e7ae3SMatthew Knepley 1487000e7ae3SMatthew Knepley Calling sequence of func: 1488000e7ae3SMatthew Knepley . func (TS ts); 1489000e7ae3SMatthew Knepley 1490000e7ae3SMatthew Knepley Level: intermediate 1491000e7ae3SMatthew Knepley 1492000e7ae3SMatthew Knepley .keywords: TS, timestep 1493000e7ae3SMatthew Knepley @*/ 14947087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1495000e7ae3SMatthew Knepley { 1496000e7ae3SMatthew Knepley PetscFunctionBegin; 14970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1498000e7ae3SMatthew Knepley ts->ops->prestep = func; 1499000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1500000e7ae3SMatthew Knepley } 1501000e7ae3SMatthew Knepley 1502e74ef692SMatthew Knepley #undef __FUNCT__ 15033f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 150409ee8438SJed Brown /*@ 15053f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 15063f2090d5SJed Brown 15073f2090d5SJed Brown Collective on TS 15083f2090d5SJed Brown 15093f2090d5SJed Brown Input Parameters: 15103f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15113f2090d5SJed Brown 15123f2090d5SJed Brown Notes: 15133f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 15143f2090d5SJed Brown so most users would not generally call this routine themselves. 15153f2090d5SJed Brown 15163f2090d5SJed Brown Level: developer 15173f2090d5SJed Brown 15183f2090d5SJed Brown .keywords: TS, timestep 15193f2090d5SJed Brown @*/ 15207087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 15213f2090d5SJed Brown { 15223f2090d5SJed Brown PetscErrorCode ierr; 15233f2090d5SJed Brown 15243f2090d5SJed Brown PetscFunctionBegin; 15250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 152672ac3e02SJed Brown if (ts->ops->prestep) { 15273f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15283f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15293f2090d5SJed Brown PetscStackPop; 1530312ce896SJed Brown } 15313f2090d5SJed Brown PetscFunctionReturn(0); 15323f2090d5SJed Brown } 15333f2090d5SJed Brown 15343f2090d5SJed Brown #undef __FUNCT__ 1535e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1536ac226902SBarry Smith /*@C 1537000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15383f2090d5SJed Brown called once at the end of each time step. 1539000e7ae3SMatthew Knepley 15403f9fe445SBarry Smith Logically Collective on TS 1541000e7ae3SMatthew Knepley 1542000e7ae3SMatthew Knepley Input Parameters: 1543000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1544000e7ae3SMatthew Knepley - func - The function 1545000e7ae3SMatthew Knepley 1546000e7ae3SMatthew Knepley Calling sequence of func: 1547000e7ae3SMatthew Knepley . func (TS ts); 1548000e7ae3SMatthew Knepley 1549000e7ae3SMatthew Knepley Level: intermediate 1550000e7ae3SMatthew Knepley 1551000e7ae3SMatthew Knepley .keywords: TS, timestep 1552000e7ae3SMatthew Knepley @*/ 15537087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1554000e7ae3SMatthew Knepley { 1555000e7ae3SMatthew Knepley PetscFunctionBegin; 15560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1557000e7ae3SMatthew Knepley ts->ops->poststep = func; 1558000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1559000e7ae3SMatthew Knepley } 1560000e7ae3SMatthew Knepley 1561e74ef692SMatthew Knepley #undef __FUNCT__ 15623f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 156309ee8438SJed Brown /*@ 15643f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15653f2090d5SJed Brown 15663f2090d5SJed Brown Collective on TS 15673f2090d5SJed Brown 15683f2090d5SJed Brown Input Parameters: 15693f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15703f2090d5SJed Brown 15713f2090d5SJed Brown Notes: 15723f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15733f2090d5SJed Brown so most users would not generally call this routine themselves. 15743f2090d5SJed Brown 15753f2090d5SJed Brown Level: developer 15763f2090d5SJed Brown 15773f2090d5SJed Brown .keywords: TS, timestep 15783f2090d5SJed Brown @*/ 15797087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15803f2090d5SJed Brown { 15813f2090d5SJed Brown PetscErrorCode ierr; 15823f2090d5SJed Brown 15833f2090d5SJed Brown PetscFunctionBegin; 15840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 158572ac3e02SJed Brown if (ts->ops->poststep) { 15863f2090d5SJed Brown PetscStackPush("TS PostStep function"); 15873f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 15883f2090d5SJed Brown PetscStackPop; 158972ac3e02SJed Brown } 15903f2090d5SJed Brown PetscFunctionReturn(0); 15913f2090d5SJed Brown } 15923f2090d5SJed Brown 1593d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1594d763cef2SBarry Smith 15954a2ae208SSatish Balay #undef __FUNCT__ 1596a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1597d763cef2SBarry Smith /*@C 1598a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1599d763cef2SBarry Smith timestep to display the iteration's progress. 1600d763cef2SBarry Smith 16013f9fe445SBarry Smith Logically Collective on TS 1602d763cef2SBarry Smith 1603d763cef2SBarry Smith Input Parameters: 1604d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1605e213d8f1SJed Brown . monitor - monitoring routine 1606329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1607b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1608b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1609b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1610d763cef2SBarry Smith 1611e213d8f1SJed Brown Calling sequence of monitor: 1612e213d8f1SJed Brown $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1613d763cef2SBarry Smith 1614d763cef2SBarry Smith + ts - the TS context 1615d763cef2SBarry Smith . steps - iteration number 16161f06c33eSBarry Smith . time - current time 1617d763cef2SBarry Smith . x - current iterate 1618d763cef2SBarry Smith - mctx - [optional] monitoring context 1619d763cef2SBarry Smith 1620d763cef2SBarry Smith Notes: 1621d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1622d763cef2SBarry Smith already has been loaded. 1623d763cef2SBarry Smith 1624025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1625025f1a04SBarry Smith 1626d763cef2SBarry Smith Level: intermediate 1627d763cef2SBarry Smith 1628d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1629d763cef2SBarry Smith 1630a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1631d763cef2SBarry Smith @*/ 1632c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1633d763cef2SBarry Smith { 1634d763cef2SBarry Smith PetscFunctionBegin; 16350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 163617186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1637d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1638329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1639d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1640d763cef2SBarry Smith PetscFunctionReturn(0); 1641d763cef2SBarry Smith } 1642d763cef2SBarry Smith 16434a2ae208SSatish Balay #undef __FUNCT__ 1644a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1645d763cef2SBarry Smith /*@C 1646a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1647d763cef2SBarry Smith 16483f9fe445SBarry Smith Logically Collective on TS 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith Input Parameters: 1651d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1652d763cef2SBarry Smith 1653d763cef2SBarry Smith Notes: 1654d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1655d763cef2SBarry Smith 1656d763cef2SBarry Smith Level: intermediate 1657d763cef2SBarry Smith 1658d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1659d763cef2SBarry Smith 1660a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1661d763cef2SBarry Smith @*/ 16627087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1663d763cef2SBarry Smith { 1664d952e501SBarry Smith PetscErrorCode ierr; 1665d952e501SBarry Smith PetscInt i; 1666d952e501SBarry Smith 1667d763cef2SBarry Smith PetscFunctionBegin; 16680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1669d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1670d952e501SBarry Smith if (ts->mdestroy[i]) { 16713c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1672d952e501SBarry Smith } 1673d952e501SBarry Smith } 1674d763cef2SBarry Smith ts->numbermonitors = 0; 1675d763cef2SBarry Smith PetscFunctionReturn(0); 1676d763cef2SBarry Smith } 1677d763cef2SBarry Smith 16784a2ae208SSatish Balay #undef __FUNCT__ 1679a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1680d8e5e3e6SSatish Balay /*@ 1681a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16825516499fSSatish Balay 16835516499fSSatish Balay Level: intermediate 168441251cbbSSatish Balay 16855516499fSSatish Balay .keywords: TS, set, monitor 16865516499fSSatish Balay 168741251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 168841251cbbSSatish Balay @*/ 1689649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1690d763cef2SBarry Smith { 1691dfbe8321SBarry Smith PetscErrorCode ierr; 1692649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1693d132466eSBarry Smith 1694d763cef2SBarry Smith PetscFunctionBegin; 1695649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1696971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 1697649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1698d763cef2SBarry Smith PetscFunctionReturn(0); 1699d763cef2SBarry Smith } 1700d763cef2SBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 1702cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 1703cd652676SJed Brown /*@ 1704cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 1705cd652676SJed Brown 1706cd652676SJed Brown Logically Collective on TS 1707cd652676SJed Brown 1708cd652676SJed Brown Input Argument: 1709cd652676SJed Brown . ts - time stepping context 1710cd652676SJed Brown 1711cd652676SJed Brown Output Argument: 1712cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 1713cd652676SJed Brown 1714cd652676SJed Brown Level: intermediate 1715cd652676SJed Brown 1716cd652676SJed Brown .keywords: TS, set 1717cd652676SJed Brown 1718cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 1719cd652676SJed Brown @*/ 1720cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 1721cd652676SJed Brown { 1722cd652676SJed Brown 1723cd652676SJed Brown PetscFunctionBegin; 1724cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1725cd652676SJed Brown ts->retain_stages = flg; 1726cd652676SJed Brown PetscFunctionReturn(0); 1727cd652676SJed Brown } 1728cd652676SJed Brown 1729cd652676SJed Brown #undef __FUNCT__ 1730cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 1731cd652676SJed Brown /*@ 1732cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 1733cd652676SJed Brown 1734cd652676SJed Brown Collective on TS 1735cd652676SJed Brown 1736cd652676SJed Brown Input Argument: 1737cd652676SJed Brown + ts - time stepping context 1738cd652676SJed Brown - t - time to interpolate to 1739cd652676SJed Brown 1740cd652676SJed Brown Output Argument: 1741cd652676SJed Brown . X - state at given time 1742cd652676SJed Brown 1743cd652676SJed Brown Notes: 1744cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 1745cd652676SJed Brown 1746cd652676SJed Brown Level: intermediate 1747cd652676SJed Brown 1748cd652676SJed Brown Developer Notes: 1749cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 1750cd652676SJed Brown 1751cd652676SJed Brown .keywords: TS, set 1752cd652676SJed Brown 1753cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 1754cd652676SJed Brown @*/ 1755cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 1756cd652676SJed Brown { 1757cd652676SJed Brown PetscErrorCode ierr; 1758cd652676SJed Brown 1759cd652676SJed Brown PetscFunctionBegin; 1760cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1761d8cd7023SBarry Smith if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime); 1762cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 1763cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 1764cd652676SJed Brown PetscFunctionReturn(0); 1765cd652676SJed Brown } 1766cd652676SJed Brown 1767cd652676SJed Brown #undef __FUNCT__ 17684a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1769d763cef2SBarry Smith /*@ 17706d9e5789SSean Farley TSStep - Steps one time step 1771d763cef2SBarry Smith 1772d763cef2SBarry Smith Collective on TS 1773d763cef2SBarry Smith 1774d763cef2SBarry Smith Input Parameter: 1775d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1776d763cef2SBarry Smith 17776d9e5789SSean Farley Level: intermediate 1778d763cef2SBarry Smith 1779d763cef2SBarry Smith .keywords: TS, timestep, solve 1780d763cef2SBarry Smith 17816d9e5789SSean Farley .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve() 1782d763cef2SBarry Smith @*/ 1783193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1784d763cef2SBarry Smith { 1785362cd11cSLisandro Dalcin PetscReal ptime_prev; 1786dfbe8321SBarry Smith PetscErrorCode ierr; 1787d763cef2SBarry Smith 1788d763cef2SBarry Smith PetscFunctionBegin; 17890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1790d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1791d405a339SMatthew Knepley 1792362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 1793362cd11cSLisandro Dalcin 1794362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 1795d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 1796193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1797d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 1798362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 1799362cd11cSLisandro Dalcin 1800362cd11cSLisandro Dalcin if (ts->reason < 0) { 1801f1b97656SJed Brown if (ts->errorifstepfailed) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 1802362cd11cSLisandro Dalcin } else if (!ts->reason) { 1803362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 1804362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 1805362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 1806362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 1807362cd11cSLisandro Dalcin } 1808362cd11cSLisandro Dalcin 1809d763cef2SBarry Smith PetscFunctionReturn(0); 1810d763cef2SBarry Smith } 1811d763cef2SBarry Smith 18124a2ae208SSatish Balay #undef __FUNCT__ 181305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 181405175c85SJed Brown /*@ 181505175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 181605175c85SJed Brown 18171c3436cfSJed Brown Collective on TS 181805175c85SJed Brown 181905175c85SJed Brown Input Arguments: 18201c3436cfSJed Brown + ts - time stepping context 18211c3436cfSJed Brown . order - desired order of accuracy 18221c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 182305175c85SJed Brown 182405175c85SJed Brown Output Arguments: 18251c3436cfSJed Brown . X - state at the end of the current step 182605175c85SJed Brown 182705175c85SJed Brown Level: advanced 182805175c85SJed Brown 18291c3436cfSJed Brown .seealso: TSStep(), TSAdapt 183005175c85SJed Brown @*/ 18311c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done) 183205175c85SJed Brown { 183305175c85SJed Brown PetscErrorCode ierr; 183405175c85SJed Brown 183505175c85SJed Brown PetscFunctionBegin; 183605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 183705175c85SJed Brown PetscValidType(ts,1); 183805175c85SJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,3); 18391c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 18401c3436cfSJed Brown ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr); 184105175c85SJed Brown PetscFunctionReturn(0); 184205175c85SJed Brown } 184305175c85SJed Brown 184405175c85SJed Brown #undef __FUNCT__ 18456a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 18466a4d4014SLisandro Dalcin /*@ 18476a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 18486a4d4014SLisandro Dalcin 18496a4d4014SLisandro Dalcin Collective on TS 18506a4d4014SLisandro Dalcin 18516a4d4014SLisandro Dalcin Input Parameter: 18526a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 1853362cd11cSLisandro Dalcin - x - the solution vector 18546a4d4014SLisandro Dalcin 18555a3a76d0SJed Brown Output Parameter: 18565a3a76d0SJed Brown . ftime - time of the state vector x upon completion 18575a3a76d0SJed Brown 18586a4d4014SLisandro Dalcin Level: beginner 18596a4d4014SLisandro Dalcin 18605a3a76d0SJed Brown Notes: 18615a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 18625a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 18635a3a76d0SJed Brown stepped over the final time. 18645a3a76d0SJed Brown 18656a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 18666a4d4014SLisandro Dalcin 18676a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 18686a4d4014SLisandro Dalcin @*/ 18695a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime) 18706a4d4014SLisandro Dalcin { 18714d7d938eSLisandro Dalcin PetscBool flg; 18724d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 18734d7d938eSLisandro Dalcin PetscViewer viewer; 18746a4d4014SLisandro Dalcin PetscErrorCode ierr; 1875f22f69f0SBarry Smith 18766a4d4014SLisandro Dalcin PetscFunctionBegin; 18770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1878193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 18795a3a76d0SJed Brown if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 18805a3a76d0SJed Brown if (!ts->vec_sol || x == ts->vec_sol) { 18815a3a76d0SJed Brown Vec y; 18825a3a76d0SJed Brown ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 18835a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 18845a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 18855a3a76d0SJed Brown } 1886362cd11cSLisandro Dalcin ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr); 18875a3a76d0SJed Brown } else { 1888193ac0bcSJed Brown ierr = TSSetSolution(ts,x);CHKERRQ(ierr); 18895a3a76d0SJed Brown } 1890b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 18916a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1892193ac0bcSJed Brown ts->steps = 0; 1893193ac0bcSJed Brown ts->linear_its = 0; 1894193ac0bcSJed Brown ts->nonlinear_its = 0; 1895c610991cSLisandro Dalcin ts->num_snes_failures = 0; 1896c610991cSLisandro Dalcin ts->reject = 0; 1897193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1898193ac0bcSJed Brown 1899193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1900193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 19015a3a76d0SJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 1902a5b82c69SSean Farley if (ftime) *ftime = ts->ptime; 1903193ac0bcSJed Brown } else { 19046a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 1905362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1906362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 1907362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 1908362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 1909362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 1910e1a7a14fSJed Brown while (!ts->reason) { 1911193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1912193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1913193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1914193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1915193ac0bcSJed Brown } 1916362cd11cSLisandro Dalcin if (ts->exact_final_time && ts->ptime > ts->max_time) { 1917a43b19c4SJed Brown ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr); 19185a3a76d0SJed Brown if (ftime) *ftime = ts->max_time; 19190574a7fbSJed Brown } else { 19200574a7fbSJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 19210574a7fbSJed Brown if (ftime) *ftime = ts->ptime; 19220574a7fbSJed Brown } 1923193ac0bcSJed Brown } 19244d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 19254d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 19264d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 19274d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 19284d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1929193ac0bcSJed Brown } 19306a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19316a4d4014SLisandro Dalcin } 19326a4d4014SLisandro Dalcin 19336a4d4014SLisandro Dalcin #undef __FUNCT__ 19344a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1935228d79bcSJed Brown /*@ 1936228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 1937228d79bcSJed Brown 1938228d79bcSJed Brown Collective on TS 1939228d79bcSJed Brown 1940228d79bcSJed Brown Input Parameters: 1941228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 1942228d79bcSJed Brown . step - step number that has just completed 1943228d79bcSJed Brown . ptime - model time of the state 1944228d79bcSJed Brown - x - state at the current model time 1945228d79bcSJed Brown 1946228d79bcSJed Brown Notes: 1947228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 1948228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 1949228d79bcSJed Brown 1950228d79bcSJed Brown Level: advanced 1951228d79bcSJed Brown 1952228d79bcSJed Brown .keywords: TS, timestep 1953228d79bcSJed Brown @*/ 1954a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1955d763cef2SBarry Smith { 19566849ba73SBarry Smith PetscErrorCode ierr; 1957a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1958d763cef2SBarry Smith 1959d763cef2SBarry Smith PetscFunctionBegin; 1960d763cef2SBarry Smith for (i=0; i<n; i++) { 1961142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1962d763cef2SBarry Smith } 1963d763cef2SBarry Smith PetscFunctionReturn(0); 1964d763cef2SBarry Smith } 1965d763cef2SBarry Smith 1966d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1967d763cef2SBarry Smith 19684a2ae208SSatish Balay #undef __FUNCT__ 1969a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1970d763cef2SBarry Smith /*@C 1971a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1972d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1973d763cef2SBarry Smith 1974d763cef2SBarry Smith Collective on TS 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith Input Parameters: 1977d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1978d763cef2SBarry Smith . label - the title to put in the title bar 19797c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1980d763cef2SBarry Smith - m, n - the screen width and height in pixels 1981d763cef2SBarry Smith 1982d763cef2SBarry Smith Output Parameter: 1983d763cef2SBarry Smith . draw - the drawing context 1984d763cef2SBarry Smith 1985d763cef2SBarry Smith Options Database Key: 1986a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1987d763cef2SBarry Smith 1988d763cef2SBarry Smith Notes: 1989a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith Level: intermediate 1992d763cef2SBarry Smith 19937c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1994d763cef2SBarry Smith 1995a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 19967c922b88SBarry Smith 1997d763cef2SBarry Smith @*/ 19987087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1999d763cef2SBarry Smith { 2000b0a32e0cSBarry Smith PetscDraw win; 2001dfbe8321SBarry Smith PetscErrorCode ierr; 2002d763cef2SBarry Smith 2003d763cef2SBarry Smith PetscFunctionBegin; 2004b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2005b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 2006b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 2007b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 2008d763cef2SBarry Smith 200952e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 2010d763cef2SBarry Smith PetscFunctionReturn(0); 2011d763cef2SBarry Smith } 2012d763cef2SBarry Smith 20134a2ae208SSatish Balay #undef __FUNCT__ 2014a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 2015a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2016d763cef2SBarry Smith { 2017b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 201887828ca2SBarry Smith PetscReal x,y = ptime; 2019dfbe8321SBarry Smith PetscErrorCode ierr; 2020d763cef2SBarry Smith 2021d763cef2SBarry Smith PetscFunctionBegin; 20227c922b88SBarry Smith if (!monctx) { 20237c922b88SBarry Smith MPI_Comm comm; 2024b0a32e0cSBarry Smith PetscViewer viewer; 20257c922b88SBarry Smith 20267c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 2027b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 2028b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 20297c922b88SBarry Smith } 20307c922b88SBarry Smith 2031b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 203287828ca2SBarry Smith x = (PetscReal)n; 2033b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2034d763cef2SBarry Smith if (n < 20 || (n % 5)) { 2035b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2036d763cef2SBarry Smith } 2037d763cef2SBarry Smith PetscFunctionReturn(0); 2038d763cef2SBarry Smith } 2039d763cef2SBarry Smith 20404a2ae208SSatish Balay #undef __FUNCT__ 2041a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 2042d763cef2SBarry Smith /*@C 2043a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 2044a6570f20SBarry Smith with TSMonitorLGCreate(). 2045d763cef2SBarry Smith 2046b0a32e0cSBarry Smith Collective on PetscDrawLG 2047d763cef2SBarry Smith 2048d763cef2SBarry Smith Input Parameter: 2049d763cef2SBarry Smith . draw - the drawing context 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Level: intermediate 2052d763cef2SBarry Smith 2053d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2054d763cef2SBarry Smith 2055a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 2056d763cef2SBarry Smith @*/ 20576bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 2058d763cef2SBarry Smith { 2059b0a32e0cSBarry Smith PetscDraw draw; 2060dfbe8321SBarry Smith PetscErrorCode ierr; 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith PetscFunctionBegin; 20636bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 20646bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 2065b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 2066d763cef2SBarry Smith PetscFunctionReturn(0); 2067d763cef2SBarry Smith } 2068d763cef2SBarry Smith 20694a2ae208SSatish Balay #undef __FUNCT__ 20704a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2071d763cef2SBarry Smith /*@ 2072d763cef2SBarry Smith TSGetTime - Gets the current time. 2073d763cef2SBarry Smith 2074d763cef2SBarry Smith Not Collective 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith Input Parameter: 2077d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith Output Parameter: 2080d763cef2SBarry Smith . t - the current time 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith Level: beginner 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith .keywords: TS, get, time 2087d763cef2SBarry Smith @*/ 20887087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2089d763cef2SBarry Smith { 2090d763cef2SBarry Smith PetscFunctionBegin; 20910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20924482741eSBarry Smith PetscValidDoublePointer(t,2); 2093d763cef2SBarry Smith *t = ts->ptime; 2094d763cef2SBarry Smith PetscFunctionReturn(0); 2095d763cef2SBarry Smith } 2096d763cef2SBarry Smith 20974a2ae208SSatish Balay #undef __FUNCT__ 20986a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 20996a4d4014SLisandro Dalcin /*@ 21006a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 21016a4d4014SLisandro Dalcin 21023f9fe445SBarry Smith Logically Collective on TS 21036a4d4014SLisandro Dalcin 21046a4d4014SLisandro Dalcin Input Parameters: 21056a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 21066a4d4014SLisandro Dalcin - time - the time 21076a4d4014SLisandro Dalcin 21086a4d4014SLisandro Dalcin Level: intermediate 21096a4d4014SLisandro Dalcin 21106a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 21116a4d4014SLisandro Dalcin 21126a4d4014SLisandro Dalcin .keywords: TS, set, time 21136a4d4014SLisandro Dalcin @*/ 21147087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 21156a4d4014SLisandro Dalcin { 21166a4d4014SLisandro Dalcin PetscFunctionBegin; 21170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2118c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 21196a4d4014SLisandro Dalcin ts->ptime = t; 21206a4d4014SLisandro Dalcin PetscFunctionReturn(0); 21216a4d4014SLisandro Dalcin } 21226a4d4014SLisandro Dalcin 21236a4d4014SLisandro Dalcin #undef __FUNCT__ 21244a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2125d763cef2SBarry Smith /*@C 2126d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2127d763cef2SBarry Smith TS options in the database. 2128d763cef2SBarry Smith 21293f9fe445SBarry Smith Logically Collective on TS 2130d763cef2SBarry Smith 2131d763cef2SBarry Smith Input Parameter: 2132d763cef2SBarry Smith + ts - The TS context 2133d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2134d763cef2SBarry Smith 2135d763cef2SBarry Smith Notes: 2136d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2137d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2138d763cef2SBarry Smith hyphen. 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Level: advanced 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2143d763cef2SBarry Smith 2144d763cef2SBarry Smith .seealso: TSSetFromOptions() 2145d763cef2SBarry Smith 2146d763cef2SBarry Smith @*/ 21477087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2148d763cef2SBarry Smith { 2149dfbe8321SBarry Smith PetscErrorCode ierr; 2150089b2837SJed Brown SNES snes; 2151d763cef2SBarry Smith 2152d763cef2SBarry Smith PetscFunctionBegin; 21530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2154d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2155089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2156089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2157d763cef2SBarry Smith PetscFunctionReturn(0); 2158d763cef2SBarry Smith } 2159d763cef2SBarry Smith 2160d763cef2SBarry Smith 21614a2ae208SSatish Balay #undef __FUNCT__ 21624a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2163d763cef2SBarry Smith /*@C 2164d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2165d763cef2SBarry Smith TS options in the database. 2166d763cef2SBarry Smith 21673f9fe445SBarry Smith Logically Collective on TS 2168d763cef2SBarry Smith 2169d763cef2SBarry Smith Input Parameter: 2170d763cef2SBarry Smith + ts - The TS context 2171d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2172d763cef2SBarry Smith 2173d763cef2SBarry Smith Notes: 2174d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2175d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2176d763cef2SBarry Smith hyphen. 2177d763cef2SBarry Smith 2178d763cef2SBarry Smith Level: advanced 2179d763cef2SBarry Smith 2180d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2181d763cef2SBarry Smith 2182d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2183d763cef2SBarry Smith 2184d763cef2SBarry Smith @*/ 21857087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2186d763cef2SBarry Smith { 2187dfbe8321SBarry Smith PetscErrorCode ierr; 2188089b2837SJed Brown SNES snes; 2189d763cef2SBarry Smith 2190d763cef2SBarry Smith PetscFunctionBegin; 21910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2192d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2193089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2194089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2195d763cef2SBarry Smith PetscFunctionReturn(0); 2196d763cef2SBarry Smith } 2197d763cef2SBarry Smith 21984a2ae208SSatish Balay #undef __FUNCT__ 21994a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2200d763cef2SBarry Smith /*@C 2201d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2202d763cef2SBarry Smith TS options in the database. 2203d763cef2SBarry Smith 2204d763cef2SBarry Smith Not Collective 2205d763cef2SBarry Smith 2206d763cef2SBarry Smith Input Parameter: 2207d763cef2SBarry Smith . ts - The TS context 2208d763cef2SBarry Smith 2209d763cef2SBarry Smith Output Parameter: 2210d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2211d763cef2SBarry Smith 2212d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2213d763cef2SBarry Smith sufficient length to hold the prefix. 2214d763cef2SBarry Smith 2215d763cef2SBarry Smith Level: intermediate 2216d763cef2SBarry Smith 2217d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2218d763cef2SBarry Smith 2219d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2220d763cef2SBarry Smith @*/ 22217087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2222d763cef2SBarry Smith { 2223dfbe8321SBarry Smith PetscErrorCode ierr; 2224d763cef2SBarry Smith 2225d763cef2SBarry Smith PetscFunctionBegin; 22260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22274482741eSBarry Smith PetscValidPointer(prefix,2); 2228d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2229d763cef2SBarry Smith PetscFunctionReturn(0); 2230d763cef2SBarry Smith } 2231d763cef2SBarry Smith 22324a2ae208SSatish Balay #undef __FUNCT__ 22334a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2234d763cef2SBarry Smith /*@C 2235d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2236d763cef2SBarry Smith 2237d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2238d763cef2SBarry Smith 2239d763cef2SBarry Smith Input Parameter: 2240d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith Output Parameters: 2243d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2244d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2245089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2246d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2247d763cef2SBarry Smith 2248d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2249d763cef2SBarry Smith 2250d763cef2SBarry Smith Level: intermediate 2251d763cef2SBarry Smith 225226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2253d763cef2SBarry Smith 2254d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2255d763cef2SBarry Smith @*/ 2256089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2257d763cef2SBarry Smith { 2258089b2837SJed Brown PetscErrorCode ierr; 2259089b2837SJed Brown SNES snes; 2260089b2837SJed Brown 2261d763cef2SBarry Smith PetscFunctionBegin; 2262089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2263089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 22643b5f76d0SSean Farley if (func) *func = ts->userops->rhsjacobian; 226526d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2266d763cef2SBarry Smith PetscFunctionReturn(0); 2267d763cef2SBarry Smith } 2268d763cef2SBarry Smith 22691713a123SBarry Smith #undef __FUNCT__ 22702eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 22712eca1d9cSJed Brown /*@C 22722eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 22732eca1d9cSJed Brown 22742eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 22752eca1d9cSJed Brown 22762eca1d9cSJed Brown Input Parameter: 22772eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 22782eca1d9cSJed Brown 22792eca1d9cSJed Brown Output Parameters: 22802eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 22812eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 22822eca1d9cSJed Brown . f - The function to compute the matrices 22832eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 22842eca1d9cSJed Brown 22852eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 22862eca1d9cSJed Brown 22872eca1d9cSJed Brown Level: advanced 22882eca1d9cSJed Brown 22892eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 22902eca1d9cSJed Brown 22912eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 22922eca1d9cSJed Brown @*/ 22937087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 22942eca1d9cSJed Brown { 2295089b2837SJed Brown PetscErrorCode ierr; 2296089b2837SJed Brown SNES snes; 2297089b2837SJed Brown 22982eca1d9cSJed Brown PetscFunctionBegin; 2299089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2300089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 23013b5f76d0SSean Farley if (f) *f = ts->userops->ijacobian; 23022eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 23032eca1d9cSJed Brown PetscFunctionReturn(0); 23042eca1d9cSJed Brown } 23052eca1d9cSJed Brown 23066083293cSBarry Smith typedef struct { 23076083293cSBarry Smith PetscViewer viewer; 23086083293cSBarry Smith Vec initialsolution; 23096083293cSBarry Smith PetscBool showinitial; 23106083293cSBarry Smith } TSMonitorSolutionCtx; 23116083293cSBarry Smith 23122eca1d9cSJed Brown #undef __FUNCT__ 2313a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 23141713a123SBarry Smith /*@C 2315a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 23161713a123SBarry Smith VecView() for the solution at each timestep 23171713a123SBarry Smith 23181713a123SBarry Smith Collective on TS 23191713a123SBarry Smith 23201713a123SBarry Smith Input Parameters: 23211713a123SBarry Smith + ts - the TS context 23221713a123SBarry Smith . step - current time-step 2323142b95e3SSatish Balay . ptime - current time 23241713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 23251713a123SBarry Smith 23261713a123SBarry Smith Level: intermediate 23271713a123SBarry Smith 23281713a123SBarry Smith .keywords: TS, vector, monitor, view 23291713a123SBarry Smith 2330a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 23311713a123SBarry Smith @*/ 23327087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 23331713a123SBarry Smith { 2334dfbe8321SBarry Smith PetscErrorCode ierr; 23356083293cSBarry Smith TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy; 23361713a123SBarry Smith 23371713a123SBarry Smith PetscFunctionBegin; 23386083293cSBarry Smith if (!step && ictx->showinitial) { 23396083293cSBarry Smith if (!ictx->initialsolution) { 23406083293cSBarry Smith ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr); 23411713a123SBarry Smith } 23426083293cSBarry Smith ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr); 23436083293cSBarry Smith } 23446083293cSBarry Smith if (ictx->showinitial) { 23456083293cSBarry Smith PetscReal pause; 23466083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 23476083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 23486083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 23496083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 23506083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 23516083293cSBarry Smith } 23526083293cSBarry Smith ierr = VecView(x,ictx->viewer);CHKERRQ(ierr); 23536083293cSBarry Smith if (ictx->showinitial) { 23546083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 23556083293cSBarry Smith } 23561713a123SBarry Smith PetscFunctionReturn(0); 23571713a123SBarry Smith } 23581713a123SBarry Smith 23591713a123SBarry Smith 23606c699258SBarry Smith #undef __FUNCT__ 23616083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy" 23626083293cSBarry Smith /*@C 23636083293cSBarry Smith TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution 23646083293cSBarry Smith 23656083293cSBarry Smith Collective on TS 23666083293cSBarry Smith 23676083293cSBarry Smith Input Parameters: 23686083293cSBarry Smith . ctx - the monitor context 23696083293cSBarry Smith 23706083293cSBarry Smith Level: intermediate 23716083293cSBarry Smith 23726083293cSBarry Smith .keywords: TS, vector, monitor, view 23736083293cSBarry Smith 23746083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 23756083293cSBarry Smith @*/ 23766083293cSBarry Smith PetscErrorCode TSMonitorSolutionDestroy(void **ctx) 23776083293cSBarry Smith { 23786083293cSBarry Smith PetscErrorCode ierr; 23796083293cSBarry Smith TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx; 23806083293cSBarry Smith 23816083293cSBarry Smith PetscFunctionBegin; 23826083293cSBarry Smith ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr); 23836083293cSBarry Smith ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr); 23846083293cSBarry Smith ierr = PetscFree(ictx);CHKERRQ(ierr); 23856083293cSBarry Smith PetscFunctionReturn(0); 23866083293cSBarry Smith } 23876083293cSBarry Smith 23886083293cSBarry Smith #undef __FUNCT__ 23896083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate" 23906083293cSBarry Smith /*@C 23916083293cSBarry Smith TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution 23926083293cSBarry Smith 23936083293cSBarry Smith Collective on TS 23946083293cSBarry Smith 23956083293cSBarry Smith Input Parameter: 23966083293cSBarry Smith . ts - time-step context 23976083293cSBarry Smith 23986083293cSBarry Smith Output Patameter: 23996083293cSBarry Smith . ctx - the monitor context 24006083293cSBarry Smith 24016083293cSBarry Smith Level: intermediate 24026083293cSBarry Smith 24036083293cSBarry Smith .keywords: TS, vector, monitor, view 24046083293cSBarry Smith 24056083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 24066083293cSBarry Smith @*/ 24076083293cSBarry Smith PetscErrorCode TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx) 24086083293cSBarry Smith { 24096083293cSBarry Smith PetscErrorCode ierr; 24106083293cSBarry Smith TSMonitorSolutionCtx *ictx; 24116083293cSBarry Smith 24126083293cSBarry Smith PetscFunctionBegin; 24136083293cSBarry Smith ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr); 24146083293cSBarry Smith *ctx = (void*)ictx; 24156083293cSBarry Smith if (!viewer) { 24166083293cSBarry Smith viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 24176083293cSBarry Smith } 24186083293cSBarry Smith ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 24196083293cSBarry Smith ictx->viewer = viewer; 24206083293cSBarry Smith ictx->showinitial = PETSC_FALSE; 24216083293cSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr); 24226083293cSBarry Smith PetscFunctionReturn(0); 24236083293cSBarry Smith } 24246083293cSBarry Smith 24256083293cSBarry Smith #undef __FUNCT__ 24266c699258SBarry Smith #define __FUNCT__ "TSSetDM" 24276c699258SBarry Smith /*@ 24286c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 24296c699258SBarry Smith 24303f9fe445SBarry Smith Logically Collective on TS and DM 24316c699258SBarry Smith 24326c699258SBarry Smith Input Parameters: 24336c699258SBarry Smith + ts - the preconditioner context 24346c699258SBarry Smith - dm - the dm 24356c699258SBarry Smith 24366c699258SBarry Smith Level: intermediate 24376c699258SBarry Smith 24386c699258SBarry Smith 24396c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 24406c699258SBarry Smith @*/ 24417087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 24426c699258SBarry Smith { 24436c699258SBarry Smith PetscErrorCode ierr; 2444089b2837SJed Brown SNES snes; 24456c699258SBarry Smith 24466c699258SBarry Smith PetscFunctionBegin; 24470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 244870663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 24496bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 24506c699258SBarry Smith ts->dm = dm; 2451089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2452089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 24536c699258SBarry Smith PetscFunctionReturn(0); 24546c699258SBarry Smith } 24556c699258SBarry Smith 24566c699258SBarry Smith #undef __FUNCT__ 24576c699258SBarry Smith #define __FUNCT__ "TSGetDM" 24586c699258SBarry Smith /*@ 24596c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 24606c699258SBarry Smith 24613f9fe445SBarry Smith Not Collective 24626c699258SBarry Smith 24636c699258SBarry Smith Input Parameter: 24646c699258SBarry Smith . ts - the preconditioner context 24656c699258SBarry Smith 24666c699258SBarry Smith Output Parameter: 24676c699258SBarry Smith . dm - the dm 24686c699258SBarry Smith 24696c699258SBarry Smith Level: intermediate 24706c699258SBarry Smith 24716c699258SBarry Smith 24726c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 24736c699258SBarry Smith @*/ 24747087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 24756c699258SBarry Smith { 24766c699258SBarry Smith PetscFunctionBegin; 24770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24786c699258SBarry Smith *dm = ts->dm; 24796c699258SBarry Smith PetscFunctionReturn(0); 24806c699258SBarry Smith } 24811713a123SBarry Smith 24820f5c6efeSJed Brown #undef __FUNCT__ 24830f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 24840f5c6efeSJed Brown /*@ 24850f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 24860f5c6efeSJed Brown 24873f9fe445SBarry Smith Logically Collective on SNES 24880f5c6efeSJed Brown 24890f5c6efeSJed Brown Input Parameter: 2490d42a1c89SJed Brown + snes - nonlinear solver 24910f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2492d42a1c89SJed Brown - ctx - user context, must be a TS 24930f5c6efeSJed Brown 24940f5c6efeSJed Brown Output Parameter: 24950f5c6efeSJed Brown . F - the nonlinear residual 24960f5c6efeSJed Brown 24970f5c6efeSJed Brown Notes: 24980f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 24990f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 25000f5c6efeSJed Brown 25010f5c6efeSJed Brown Level: advanced 25020f5c6efeSJed Brown 25030f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 25040f5c6efeSJed Brown @*/ 25057087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 25060f5c6efeSJed Brown { 25070f5c6efeSJed Brown TS ts = (TS)ctx; 25080f5c6efeSJed Brown PetscErrorCode ierr; 25090f5c6efeSJed Brown 25100f5c6efeSJed Brown PetscFunctionBegin; 25110f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25120f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 25130f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 25140f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 25150f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 25160f5c6efeSJed Brown PetscFunctionReturn(0); 25170f5c6efeSJed Brown } 25180f5c6efeSJed Brown 25190f5c6efeSJed Brown #undef __FUNCT__ 25200f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 25210f5c6efeSJed Brown /*@ 25220f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 25230f5c6efeSJed Brown 25240f5c6efeSJed Brown Collective on SNES 25250f5c6efeSJed Brown 25260f5c6efeSJed Brown Input Parameter: 25270f5c6efeSJed Brown + snes - nonlinear solver 25280f5c6efeSJed Brown . X - the current state at which to evaluate the residual 25290f5c6efeSJed Brown - ctx - user context, must be a TS 25300f5c6efeSJed Brown 25310f5c6efeSJed Brown Output Parameter: 25320f5c6efeSJed Brown + A - the Jacobian 25330f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 25340f5c6efeSJed Brown - flag - indicates any structure change in the matrix 25350f5c6efeSJed Brown 25360f5c6efeSJed Brown Notes: 25370f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 25380f5c6efeSJed Brown 25390f5c6efeSJed Brown Level: developer 25400f5c6efeSJed Brown 25410f5c6efeSJed Brown .seealso: SNESSetJacobian() 25420f5c6efeSJed Brown @*/ 25437087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 25440f5c6efeSJed Brown { 25450f5c6efeSJed Brown TS ts = (TS)ctx; 25460f5c6efeSJed Brown PetscErrorCode ierr; 25470f5c6efeSJed Brown 25480f5c6efeSJed Brown PetscFunctionBegin; 25490f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25500f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 25510f5c6efeSJed Brown PetscValidPointer(A,3); 25520f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 25530f5c6efeSJed Brown PetscValidPointer(B,4); 25540f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 25550f5c6efeSJed Brown PetscValidPointer(flag,5); 25560f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 25570f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 25580f5c6efeSJed Brown PetscFunctionReturn(0); 25590f5c6efeSJed Brown } 2560325fc9f4SBarry Smith 25610e4ef248SJed Brown #undef __FUNCT__ 25620e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 25630e4ef248SJed Brown /*@C 25640e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 25650e4ef248SJed Brown 25660e4ef248SJed Brown Collective on TS 25670e4ef248SJed Brown 25680e4ef248SJed Brown Input Arguments: 25690e4ef248SJed Brown + ts - time stepping context 25700e4ef248SJed Brown . t - time at which to evaluate 25710e4ef248SJed Brown . X - state at which to evaluate 25720e4ef248SJed Brown - ctx - context 25730e4ef248SJed Brown 25740e4ef248SJed Brown Output Arguments: 25750e4ef248SJed Brown . F - right hand side 25760e4ef248SJed Brown 25770e4ef248SJed Brown Level: intermediate 25780e4ef248SJed Brown 25790e4ef248SJed Brown Notes: 25800e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 25810e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 25820e4ef248SJed Brown 25830e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 25840e4ef248SJed Brown @*/ 25850e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 25860e4ef248SJed Brown { 25870e4ef248SJed Brown PetscErrorCode ierr; 25880e4ef248SJed Brown Mat Arhs,Brhs; 25890e4ef248SJed Brown MatStructure flg2; 25900e4ef248SJed Brown 25910e4ef248SJed Brown PetscFunctionBegin; 25920e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 25930e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 25940e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 25950e4ef248SJed Brown PetscFunctionReturn(0); 25960e4ef248SJed Brown } 25970e4ef248SJed Brown 25980e4ef248SJed Brown #undef __FUNCT__ 25990e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 26000e4ef248SJed Brown /*@C 26010e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 26020e4ef248SJed Brown 26030e4ef248SJed Brown Collective on TS 26040e4ef248SJed Brown 26050e4ef248SJed Brown Input Arguments: 26060e4ef248SJed Brown + ts - time stepping context 26070e4ef248SJed Brown . t - time at which to evaluate 26080e4ef248SJed Brown . X - state at which to evaluate 26090e4ef248SJed Brown - ctx - context 26100e4ef248SJed Brown 26110e4ef248SJed Brown Output Arguments: 26120e4ef248SJed Brown + A - pointer to operator 26130e4ef248SJed Brown . B - pointer to preconditioning matrix 26140e4ef248SJed Brown - flg - matrix structure flag 26150e4ef248SJed Brown 26160e4ef248SJed Brown Level: intermediate 26170e4ef248SJed Brown 26180e4ef248SJed Brown Notes: 26190e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 26200e4ef248SJed Brown 26210e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 26220e4ef248SJed Brown @*/ 26230e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 26240e4ef248SJed Brown { 26250e4ef248SJed Brown 26260e4ef248SJed Brown PetscFunctionBegin; 26270e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 26280e4ef248SJed Brown PetscFunctionReturn(0); 26290e4ef248SJed Brown } 26300e4ef248SJed Brown 26310026cea9SSean Farley #undef __FUNCT__ 26320026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 26330026cea9SSean Farley /*@C 26340026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 26350026cea9SSean Farley 26360026cea9SSean Farley Collective on TS 26370026cea9SSean Farley 26380026cea9SSean Farley Input Arguments: 26390026cea9SSean Farley + ts - time stepping context 26400026cea9SSean Farley . t - time at which to evaluate 26410026cea9SSean Farley . X - state at which to evaluate 26420026cea9SSean Farley . Xdot - time derivative of state vector 26430026cea9SSean Farley - ctx - context 26440026cea9SSean Farley 26450026cea9SSean Farley Output Arguments: 26460026cea9SSean Farley . F - left hand side 26470026cea9SSean Farley 26480026cea9SSean Farley Level: intermediate 26490026cea9SSean Farley 26500026cea9SSean Farley Notes: 26510026cea9SSean 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 26520026cea9SSean Farley user is required to write their own TSComputeIFunction. 26530026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 26540026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 26550026cea9SSean Farley 26560026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 26570026cea9SSean Farley @*/ 26580026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 26590026cea9SSean Farley { 26600026cea9SSean Farley PetscErrorCode ierr; 26610026cea9SSean Farley Mat A,B; 26620026cea9SSean Farley MatStructure flg2; 26630026cea9SSean Farley 26640026cea9SSean Farley PetscFunctionBegin; 26650026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 26660026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 26670026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 26680026cea9SSean Farley PetscFunctionReturn(0); 26690026cea9SSean Farley } 26700026cea9SSean Farley 26710026cea9SSean Farley #undef __FUNCT__ 26720026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 26730026cea9SSean Farley /*@C 26740026cea9SSean Farley TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 26750026cea9SSean Farley 26760026cea9SSean Farley Collective on TS 26770026cea9SSean Farley 26780026cea9SSean Farley Input Arguments: 26790026cea9SSean Farley + ts - time stepping context 26800026cea9SSean Farley . t - time at which to evaluate 26810026cea9SSean Farley . X - state at which to evaluate 26820026cea9SSean Farley . Xdot - time derivative of state vector 26830026cea9SSean Farley . shift - shift to apply 26840026cea9SSean Farley - ctx - context 26850026cea9SSean Farley 26860026cea9SSean Farley Output Arguments: 26870026cea9SSean Farley + A - pointer to operator 26880026cea9SSean Farley . B - pointer to preconditioning matrix 26890026cea9SSean Farley - flg - matrix structure flag 26900026cea9SSean Farley 26910026cea9SSean Farley Level: intermediate 26920026cea9SSean Farley 26930026cea9SSean Farley Notes: 26940026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 26950026cea9SSean Farley 26960026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 26970026cea9SSean Farley @*/ 26980026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 26990026cea9SSean Farley { 27000026cea9SSean Farley 27010026cea9SSean Farley PetscFunctionBegin; 27020026cea9SSean Farley *flg = SAME_PRECONDITIONER; 27030026cea9SSean Farley PetscFunctionReturn(0); 27040026cea9SSean Farley } 27050026cea9SSean Farley 27064af1b03aSJed Brown 27074af1b03aSJed Brown #undef __FUNCT__ 27084af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 27094af1b03aSJed Brown /*@ 27104af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 27114af1b03aSJed Brown 27124af1b03aSJed Brown Not Collective 27134af1b03aSJed Brown 27144af1b03aSJed Brown Input Parameter: 27154af1b03aSJed Brown . ts - the TS context 27164af1b03aSJed Brown 27174af1b03aSJed Brown Output Parameter: 27184af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 27194af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 27204af1b03aSJed Brown 27214af1b03aSJed Brown Level: intermediate 27224af1b03aSJed Brown 2723cd652676SJed Brown Notes: 2724cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 27254af1b03aSJed Brown 27264af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 27274af1b03aSJed Brown 27284af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 27294af1b03aSJed Brown @*/ 27304af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 27314af1b03aSJed Brown { 27324af1b03aSJed Brown PetscFunctionBegin; 27334af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27344af1b03aSJed Brown PetscValidPointer(reason,2); 27354af1b03aSJed Brown *reason = ts->reason; 27364af1b03aSJed Brown PetscFunctionReturn(0); 27374af1b03aSJed Brown } 27384af1b03aSJed Brown 2739fb1732b5SBarry Smith #undef __FUNCT__ 27409f67acb7SJed Brown #define __FUNCT__ "TSGetNonlinearSolveIterations" 27419f67acb7SJed Brown /*@ 274201c7e25bSJed Brown TSGetNonlinearSolveIterations - Gets the total number of linear iterations 27439f67acb7SJed Brown used by the time integrator. 27449f67acb7SJed Brown 27459f67acb7SJed Brown Not Collective 27469f67acb7SJed Brown 27479f67acb7SJed Brown Input Parameter: 27489f67acb7SJed Brown . ts - TS context 27499f67acb7SJed Brown 27509f67acb7SJed Brown Output Parameter: 27519f67acb7SJed Brown . nits - number of nonlinear iterations 27529f67acb7SJed Brown 27539f67acb7SJed Brown Notes: 27549f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 27559f67acb7SJed Brown 27569f67acb7SJed Brown Level: intermediate 27579f67acb7SJed Brown 27589f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 27599f67acb7SJed Brown 27609f67acb7SJed Brown .seealso: TSGetLinearSolveIterations() 27619f67acb7SJed Brown @*/ 27629f67acb7SJed Brown PetscErrorCode TSGetNonlinearSolveIterations(TS ts,PetscInt *nits) 27639f67acb7SJed Brown { 27649f67acb7SJed Brown PetscFunctionBegin; 27659f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27669f67acb7SJed Brown PetscValidIntPointer(nits,2); 27679f67acb7SJed Brown *nits = ts->nonlinear_its; 27689f67acb7SJed Brown PetscFunctionReturn(0); 27699f67acb7SJed Brown } 27709f67acb7SJed Brown 27719f67acb7SJed Brown #undef __FUNCT__ 27729f67acb7SJed Brown #define __FUNCT__ "TSGetLinearSolveIterations" 27739f67acb7SJed Brown /*@ 27749f67acb7SJed Brown TSGetLinearSolveIterations - Gets the total number of linear iterations 27759f67acb7SJed Brown used by the time integrator. 27769f67acb7SJed Brown 27779f67acb7SJed Brown Not Collective 27789f67acb7SJed Brown 27799f67acb7SJed Brown Input Parameter: 27809f67acb7SJed Brown . ts - TS context 27819f67acb7SJed Brown 27829f67acb7SJed Brown Output Parameter: 27839f67acb7SJed Brown . lits - number of linear iterations 27849f67acb7SJed Brown 27859f67acb7SJed Brown Notes: 27869f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 27879f67acb7SJed Brown 27889f67acb7SJed Brown Level: intermediate 27899f67acb7SJed Brown 27909f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 27919f67acb7SJed Brown 27929f67acb7SJed Brown .seealso: TSGetNonlinearSolveIterations(), SNESGetLinearSolveIterations() 27939f67acb7SJed Brown @*/ 27949f67acb7SJed Brown PetscErrorCode TSGetLinearSolveIterations(TS ts,PetscInt *lits) 27959f67acb7SJed Brown { 27969f67acb7SJed Brown PetscFunctionBegin; 27979f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27989f67acb7SJed Brown PetscValidIntPointer(lits,2); 27999f67acb7SJed Brown *lits = ts->linear_its; 28009f67acb7SJed Brown PetscFunctionReturn(0); 28019f67acb7SJed Brown } 28029f67acb7SJed Brown 28039f67acb7SJed Brown #undef __FUNCT__ 2804fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 2805fb1732b5SBarry Smith /*@C 2806fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 2807fb1732b5SBarry Smith 2808fb1732b5SBarry Smith Collective on TS 2809fb1732b5SBarry Smith 2810fb1732b5SBarry Smith Input Parameters: 2811fb1732b5SBarry Smith + ts - the TS context 2812fb1732b5SBarry Smith . step - current time-step 2813fb1732b5SBarry Smith . ptime - current time 2814fb1732b5SBarry Smith - viewer - binary viewer 2815fb1732b5SBarry Smith 2816fb1732b5SBarry Smith Level: intermediate 2817fb1732b5SBarry Smith 2818fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 2819fb1732b5SBarry Smith 2820fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2821fb1732b5SBarry Smith @*/ 2822fb1732b5SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 2823fb1732b5SBarry Smith { 2824fb1732b5SBarry Smith PetscErrorCode ierr; 2825fb1732b5SBarry Smith PetscViewer viewer = (PetscViewer)dummy; 2826fb1732b5SBarry Smith 2827fb1732b5SBarry Smith PetscFunctionBegin; 2828fb1732b5SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 2829fb1732b5SBarry Smith PetscFunctionReturn(0); 2830fb1732b5SBarry Smith } 2831fb1732b5SBarry Smith 283284df9cb4SJed Brown #undef __FUNCT__ 283384df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt" 283484df9cb4SJed Brown /*@ 283584df9cb4SJed Brown TSGetAdapt - Get the adaptive controller context for the current method 283684df9cb4SJed Brown 283784df9cb4SJed Brown Not Collective 283884df9cb4SJed Brown 283984df9cb4SJed Brown Input Arguments: 284084df9cb4SJed Brown 284184df9cb4SJed Brown Output Arguments: 284284df9cb4SJed Brown 284384df9cb4SJed Brown Level: intermediate 284484df9cb4SJed Brown 284584df9cb4SJed Brown .seealso: 284684df9cb4SJed Brown @*/ 284784df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 284884df9cb4SJed Brown { 284984df9cb4SJed Brown PetscErrorCode ierr; 285084df9cb4SJed Brown 285184df9cb4SJed Brown PetscFunctionBegin; 285284df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 285384df9cb4SJed Brown PetscValidPointer(adapt,2); 285484df9cb4SJed Brown if (!ts->adapt) { 285584df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 28561c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 28571c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 285884df9cb4SJed Brown } 285984df9cb4SJed Brown *adapt = ts->adapt; 286084df9cb4SJed Brown PetscFunctionReturn(0); 286184df9cb4SJed Brown } 2862d6ebe24aSShri Abhyankar 2863d6ebe24aSShri Abhyankar #undef __FUNCT__ 28641c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 28651c3436cfSJed Brown /*@ 28661c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 28671c3436cfSJed Brown 28681c3436cfSJed Brown Logically Collective 28691c3436cfSJed Brown 28701c3436cfSJed Brown Input Arguments: 28711c3436cfSJed Brown + ts - time integration context 28721c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 28731c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 28741c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 28751c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 28761c3436cfSJed Brown 28771c3436cfSJed Brown Level: beginner 28781c3436cfSJed Brown 28791c3436cfSJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS() 28801c3436cfSJed Brown @*/ 28811c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 28821c3436cfSJed Brown { 28831c3436cfSJed Brown PetscErrorCode ierr; 28841c3436cfSJed Brown 28851c3436cfSJed Brown PetscFunctionBegin; 28861c3436cfSJed Brown if (atol != PETSC_DECIDE) ts->atol = atol; 28871c3436cfSJed Brown if (vatol) { 28881c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 28891c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 28901c3436cfSJed Brown ts->vatol = vatol; 28911c3436cfSJed Brown } 28921c3436cfSJed Brown if (rtol != PETSC_DECIDE) ts->rtol = rtol; 28931c3436cfSJed Brown if (vrtol) { 28941c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 28951c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 28961c3436cfSJed Brown ts->vrtol = vrtol; 28971c3436cfSJed Brown } 28981c3436cfSJed Brown PetscFunctionReturn(0); 28991c3436cfSJed Brown } 29001c3436cfSJed Brown 29011c3436cfSJed Brown #undef __FUNCT__ 29021c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 29031c3436cfSJed Brown /*@ 29041c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 29051c3436cfSJed Brown 29061c3436cfSJed Brown Collective on TS 29071c3436cfSJed Brown 29081c3436cfSJed Brown Input Arguments: 29091c3436cfSJed Brown + ts - time stepping context 29101c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 29111c3436cfSJed Brown 29121c3436cfSJed Brown Output Arguments: 29131c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 29141c3436cfSJed Brown 29151c3436cfSJed Brown Level: developer 29161c3436cfSJed Brown 29171c3436cfSJed Brown .seealso: TSSetTolerances() 29181c3436cfSJed Brown @*/ 29191c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 29201c3436cfSJed Brown { 29211c3436cfSJed Brown PetscErrorCode ierr; 29221c3436cfSJed Brown PetscInt i,n,N; 29231c3436cfSJed Brown const PetscScalar *x,*y; 29241c3436cfSJed Brown Vec X; 29251c3436cfSJed Brown PetscReal sum,gsum; 29261c3436cfSJed Brown 29271c3436cfSJed Brown PetscFunctionBegin; 29281c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29291c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 29301c3436cfSJed Brown PetscValidPointer(norm,3); 29311c3436cfSJed Brown X = ts->vec_sol; 29321c3436cfSJed Brown PetscCheckSameTypeAndComm(X,1,Y,2); 29331c3436cfSJed Brown if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 29341c3436cfSJed Brown 29351c3436cfSJed Brown /* This is simple to implement, just not done yet */ 29361c3436cfSJed Brown if (ts->vatol || ts->vrtol) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"No support for vector scaling yet"); 29371c3436cfSJed Brown 29381c3436cfSJed Brown ierr = VecGetSize(X,&N);CHKERRQ(ierr); 29391c3436cfSJed Brown ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); 29401c3436cfSJed Brown ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); 29411c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 29421c3436cfSJed Brown sum = 0.; 29431c3436cfSJed Brown for (i=0; i<n; i++) { 29441c3436cfSJed Brown PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 29451c3436cfSJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 29461c3436cfSJed Brown } 29471c3436cfSJed Brown ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); 29481c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 29491c3436cfSJed Brown 29501c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 29511c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 29521c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 29531c3436cfSJed Brown PetscFunctionReturn(0); 29541c3436cfSJed Brown } 29551c3436cfSJed Brown 29561c3436cfSJed Brown #undef __FUNCT__ 2957*8d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 2958*8d59e960SJed Brown /*@ 2959*8d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 2960*8d59e960SJed Brown 2961*8d59e960SJed Brown Logically Collective on TS 2962*8d59e960SJed Brown 2963*8d59e960SJed Brown Input Arguments: 2964*8d59e960SJed Brown + ts - time stepping context 2965*8d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 2966*8d59e960SJed Brown 2967*8d59e960SJed Brown Note: 2968*8d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 2969*8d59e960SJed Brown 2970*8d59e960SJed Brown Level: intermediate 2971*8d59e960SJed Brown 2972*8d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 2973*8d59e960SJed Brown @*/ 2974*8d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 2975*8d59e960SJed Brown { 2976*8d59e960SJed Brown 2977*8d59e960SJed Brown PetscFunctionBegin; 2978*8d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2979*8d59e960SJed Brown ts->cfltime_local = cfltime; 2980*8d59e960SJed Brown ts->cfltime = -1.; 2981*8d59e960SJed Brown PetscFunctionReturn(0); 2982*8d59e960SJed Brown } 2983*8d59e960SJed Brown 2984*8d59e960SJed Brown #undef __FUNCT__ 2985*8d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 2986*8d59e960SJed Brown /*@ 2987*8d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 2988*8d59e960SJed Brown 2989*8d59e960SJed Brown Collective on TS 2990*8d59e960SJed Brown 2991*8d59e960SJed Brown Input Arguments: 2992*8d59e960SJed Brown . ts - time stepping context 2993*8d59e960SJed Brown 2994*8d59e960SJed Brown Output Arguments: 2995*8d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 2996*8d59e960SJed Brown 2997*8d59e960SJed Brown Level: advanced 2998*8d59e960SJed Brown 2999*8d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 3000*8d59e960SJed Brown @*/ 3001*8d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 3002*8d59e960SJed Brown { 3003*8d59e960SJed Brown PetscErrorCode ierr; 3004*8d59e960SJed Brown 3005*8d59e960SJed Brown PetscFunctionBegin; 3006*8d59e960SJed Brown if (ts->cfltime < 0) { 3007*8d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 3008*8d59e960SJed Brown } 3009*8d59e960SJed Brown *cfltime = ts->cfltime; 3010*8d59e960SJed Brown PetscFunctionReturn(0); 3011*8d59e960SJed Brown } 3012*8d59e960SJed Brown 3013*8d59e960SJed Brown #undef __FUNCT__ 3014d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3015d6ebe24aSShri Abhyankar /*@ 3016d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3017d6ebe24aSShri Abhyankar 3018d6ebe24aSShri Abhyankar Input Parameters: 3019d6ebe24aSShri Abhyankar . ts - the TS context. 3020d6ebe24aSShri Abhyankar . xl - lower bound. 3021d6ebe24aSShri Abhyankar . xu - upper bound. 3022d6ebe24aSShri Abhyankar 3023d6ebe24aSShri Abhyankar Notes: 3024d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 3025d6ebe24aSShri Abhyankar SNES_VI_INF and SNES_VI_NINF respectively during SNESSetUp(). 3026d6ebe24aSShri Abhyankar 30272bd2b0e6SSatish Balay Level: advanced 30282bd2b0e6SSatish Balay 3029d6ebe24aSShri Abhyankar @*/ 3030d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3031d6ebe24aSShri Abhyankar { 3032d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3033d6ebe24aSShri Abhyankar SNES snes; 3034d6ebe24aSShri Abhyankar 3035d6ebe24aSShri Abhyankar PetscFunctionBegin; 3036d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3037d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3038d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3039d6ebe24aSShri Abhyankar } 3040d6ebe24aSShri Abhyankar 3041325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3042c6db04a5SJed Brown #include <mex.h> 3043325fc9f4SBarry Smith 3044325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3045325fc9f4SBarry Smith 3046325fc9f4SBarry Smith #undef __FUNCT__ 3047325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3048325fc9f4SBarry Smith /* 3049325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3050325fc9f4SBarry Smith TSSetFunctionMatlab(). 3051325fc9f4SBarry Smith 3052325fc9f4SBarry Smith Collective on TS 3053325fc9f4SBarry Smith 3054325fc9f4SBarry Smith Input Parameters: 3055325fc9f4SBarry Smith + snes - the TS context 3056325fc9f4SBarry Smith - x - input vector 3057325fc9f4SBarry Smith 3058325fc9f4SBarry Smith Output Parameter: 3059325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3060325fc9f4SBarry Smith 3061325fc9f4SBarry Smith Notes: 3062325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3063325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3064325fc9f4SBarry Smith themselves. 3065325fc9f4SBarry Smith 3066325fc9f4SBarry Smith Level: developer 3067325fc9f4SBarry Smith 3068325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3069325fc9f4SBarry Smith 3070325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3071325fc9f4SBarry Smith */ 30727087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 3073325fc9f4SBarry Smith { 3074325fc9f4SBarry Smith PetscErrorCode ierr; 3075325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3076325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3077325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3078325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3079325fc9f4SBarry Smith 3080325fc9f4SBarry Smith PetscFunctionBegin; 3081325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3082325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3083325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 3084325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3085325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 3086325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3087325fc9f4SBarry Smith 3088325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3089325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3090880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 3091325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 3092325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3093325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3094325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3095325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3096325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3097325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3098325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3099325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3100325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3101325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3102325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3103325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3104325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3105325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3106325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3107325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3108325fc9f4SBarry Smith PetscFunctionReturn(0); 3109325fc9f4SBarry Smith } 3110325fc9f4SBarry Smith 3111325fc9f4SBarry Smith 3112325fc9f4SBarry Smith #undef __FUNCT__ 3113325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3114325fc9f4SBarry Smith /* 3115325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3116325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3117e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3118325fc9f4SBarry Smith 3119325fc9f4SBarry Smith Logically Collective on TS 3120325fc9f4SBarry Smith 3121325fc9f4SBarry Smith Input Parameters: 3122325fc9f4SBarry Smith + ts - the TS context 3123325fc9f4SBarry Smith - func - function evaluation routine 3124325fc9f4SBarry Smith 3125325fc9f4SBarry Smith Calling sequence of func: 3126325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 3127325fc9f4SBarry Smith 3128325fc9f4SBarry Smith Level: beginner 3129325fc9f4SBarry Smith 3130325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3131325fc9f4SBarry Smith 3132325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3133325fc9f4SBarry Smith */ 3134cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3135325fc9f4SBarry Smith { 3136325fc9f4SBarry Smith PetscErrorCode ierr; 3137325fc9f4SBarry Smith TSMatlabContext *sctx; 3138325fc9f4SBarry Smith 3139325fc9f4SBarry Smith PetscFunctionBegin; 3140325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3141325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3142325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3143325fc9f4SBarry Smith /* 3144325fc9f4SBarry Smith This should work, but it doesn't 3145325fc9f4SBarry Smith sctx->ctx = ctx; 3146325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3147325fc9f4SBarry Smith */ 3148325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3149cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3150325fc9f4SBarry Smith PetscFunctionReturn(0); 3151325fc9f4SBarry Smith } 3152325fc9f4SBarry Smith 3153325fc9f4SBarry Smith #undef __FUNCT__ 3154325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 3155325fc9f4SBarry Smith /* 3156325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 3157325fc9f4SBarry Smith TSSetJacobianMatlab(). 3158325fc9f4SBarry Smith 3159325fc9f4SBarry Smith Collective on TS 3160325fc9f4SBarry Smith 3161325fc9f4SBarry Smith Input Parameters: 3162cdcf91faSSean Farley + ts - the TS context 3163325fc9f4SBarry Smith . x - input vector 3164325fc9f4SBarry Smith . A, B - the matrices 3165325fc9f4SBarry Smith - ctx - user context 3166325fc9f4SBarry Smith 3167325fc9f4SBarry Smith Output Parameter: 3168325fc9f4SBarry Smith . flag - structure of the matrix 3169325fc9f4SBarry Smith 3170325fc9f4SBarry Smith Level: developer 3171325fc9f4SBarry Smith 3172325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3173325fc9f4SBarry Smith 3174325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3175325fc9f4SBarry Smith @*/ 3176cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3177325fc9f4SBarry Smith { 3178325fc9f4SBarry Smith PetscErrorCode ierr; 3179325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3180325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 3181325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 3182325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 3183325fc9f4SBarry Smith 3184325fc9f4SBarry Smith PetscFunctionBegin; 3185cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3186325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3187325fc9f4SBarry Smith 3188325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 3189325fc9f4SBarry Smith 3190cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3191325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3192325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 3193325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 3194325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 3195325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3196325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 3197325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3198325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3199325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 3200325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 3201325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 3202325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 3203325fc9f4SBarry Smith prhs[8] = sctx->ctx; 3204325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 3205325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3206325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 3207325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3208325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3209325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3210325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3211325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3212325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3213325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 3214325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 3215325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3216325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 3217325fc9f4SBarry Smith PetscFunctionReturn(0); 3218325fc9f4SBarry Smith } 3219325fc9f4SBarry Smith 3220325fc9f4SBarry Smith 3221325fc9f4SBarry Smith #undef __FUNCT__ 3222325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 3223325fc9f4SBarry Smith /* 3224325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 3225e3c5b3baSBarry 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 3226325fc9f4SBarry Smith 3227325fc9f4SBarry Smith Logically Collective on TS 3228325fc9f4SBarry Smith 3229325fc9f4SBarry Smith Input Parameters: 3230cdcf91faSSean Farley + ts - the TS context 3231325fc9f4SBarry Smith . A,B - Jacobian matrices 3232325fc9f4SBarry Smith . func - function evaluation routine 3233325fc9f4SBarry Smith - ctx - user context 3234325fc9f4SBarry Smith 3235325fc9f4SBarry Smith Calling sequence of func: 3236cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 3237325fc9f4SBarry Smith 3238325fc9f4SBarry Smith 3239325fc9f4SBarry Smith Level: developer 3240325fc9f4SBarry Smith 3241325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3242325fc9f4SBarry Smith 3243325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3244325fc9f4SBarry Smith */ 3245cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 3246325fc9f4SBarry Smith { 3247325fc9f4SBarry Smith PetscErrorCode ierr; 3248325fc9f4SBarry Smith TSMatlabContext *sctx; 3249325fc9f4SBarry Smith 3250325fc9f4SBarry Smith PetscFunctionBegin; 3251325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3252325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3253325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3254325fc9f4SBarry Smith /* 3255325fc9f4SBarry Smith This should work, but it doesn't 3256325fc9f4SBarry Smith sctx->ctx = ctx; 3257325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3258325fc9f4SBarry Smith */ 3259325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3260cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 3261325fc9f4SBarry Smith PetscFunctionReturn(0); 3262325fc9f4SBarry Smith } 3263325fc9f4SBarry Smith 3264b5b1a830SBarry Smith #undef __FUNCT__ 3265b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 3266b5b1a830SBarry Smith /* 3267b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 3268b5b1a830SBarry Smith 3269b5b1a830SBarry Smith Collective on TS 3270b5b1a830SBarry Smith 3271b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3272b5b1a830SBarry Smith @*/ 3273cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 3274b5b1a830SBarry Smith { 3275b5b1a830SBarry Smith PetscErrorCode ierr; 3276b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3277a530c242SBarry Smith int nlhs = 1,nrhs = 6; 3278b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 3279b5b1a830SBarry Smith long long int lx = 0,ls = 0; 3280b5b1a830SBarry Smith 3281b5b1a830SBarry Smith PetscFunctionBegin; 3282cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3283b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3284b5b1a830SBarry Smith 3285cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3286b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3287b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3288b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 3289b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 3290b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 3291b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 3292b5b1a830SBarry Smith prhs[5] = sctx->ctx; 3293b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 3294b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3295b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 3296b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 3297b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 3298b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 3299b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 3300b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 3301b5b1a830SBarry Smith PetscFunctionReturn(0); 3302b5b1a830SBarry Smith } 3303b5b1a830SBarry Smith 3304b5b1a830SBarry Smith 3305b5b1a830SBarry Smith #undef __FUNCT__ 3306b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 3307b5b1a830SBarry Smith /* 3308b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 3309b5b1a830SBarry Smith 3310b5b1a830SBarry Smith Level: developer 3311b5b1a830SBarry Smith 3312b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 3313b5b1a830SBarry Smith 3314b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3315b5b1a830SBarry Smith */ 3316cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 3317b5b1a830SBarry Smith { 3318b5b1a830SBarry Smith PetscErrorCode ierr; 3319b5b1a830SBarry Smith TSMatlabContext *sctx; 3320b5b1a830SBarry Smith 3321b5b1a830SBarry Smith PetscFunctionBegin; 3322b5b1a830SBarry Smith /* currently sctx is memory bleed */ 3323b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3324b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3325b5b1a830SBarry Smith /* 3326b5b1a830SBarry Smith This should work, but it doesn't 3327b5b1a830SBarry Smith sctx->ctx = ctx; 3328b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3329b5b1a830SBarry Smith */ 3330b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3331cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3332b5b1a830SBarry Smith PetscFunctionReturn(0); 3333b5b1a830SBarry Smith } 3334325fc9f4SBarry Smith #endif 3335