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 603bca7d26SBarry Smith . -ts_final_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); 883bca7d26SBarry Smith ierr = PetscOptionsReal("-ts_final_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 } 132ed81e22dSJed Brown opt = PETSC_FALSE; 133ed81e22dSJed Brown ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 134ed81e22dSJed Brown if (flg) { 135ed81e22dSJed Brown const char *ptr,*ptr2; 136ed81e22dSJed Brown char *filetemplate; 137ed81e22dSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 138ed81e22dSJed Brown /* Do some cursory validation of the input. */ 139ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 140ed81e22dSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 141ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 142ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 143ed81e22dSJed Brown if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 144ed81e22dSJed Brown if (ptr2) break; 145ed81e22dSJed Brown } 146ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 147ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 148ed81e22dSJed Brown } 149bdad233fSMatthew Knepley 1501c3436cfSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 1511c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 1521c3436cfSJed Brown 153d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 154d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 155d52bd9f3SBarry Smith 156bdad233fSMatthew Knepley /* Handle specific TS options */ 157abc0a331SBarry Smith if (ts->ops->setfromoptions) { 158bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 159bdad233fSMatthew Knepley } 1605d973c19SBarry Smith 1615d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1625d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 163bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 164bdad233fSMatthew Knepley PetscFunctionReturn(0); 165bdad233fSMatthew Knepley } 166bdad233fSMatthew Knepley 167bdad233fSMatthew Knepley #undef __FUNCT__ 168cdcf91faSSean Farley #undef __FUNCT__ 1694a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 170a7a1495cSBarry Smith /*@ 1718c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 172a7a1495cSBarry Smith set with TSSetRHSJacobian(). 173a7a1495cSBarry Smith 174a7a1495cSBarry Smith Collective on TS and Vec 175a7a1495cSBarry Smith 176a7a1495cSBarry Smith Input Parameters: 177316643e7SJed Brown + ts - the TS context 178a7a1495cSBarry Smith . t - current timestep 179a7a1495cSBarry Smith - x - input vector 180a7a1495cSBarry Smith 181a7a1495cSBarry Smith Output Parameters: 182a7a1495cSBarry Smith + A - Jacobian matrix 183a7a1495cSBarry Smith . B - optional preconditioning matrix 184a7a1495cSBarry Smith - flag - flag indicating matrix structure 185a7a1495cSBarry Smith 186a7a1495cSBarry Smith Notes: 187a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 188a7a1495cSBarry Smith is used internally within the nonlinear solvers. 189a7a1495cSBarry Smith 19094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 191a7a1495cSBarry Smith flag parameter. 192a7a1495cSBarry Smith 193a7a1495cSBarry Smith Level: developer 194a7a1495cSBarry Smith 195a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 196a7a1495cSBarry Smith 19794b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 198a7a1495cSBarry Smith @*/ 1997087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 200a7a1495cSBarry Smith { 201dfbe8321SBarry Smith PetscErrorCode ierr; 2020e4ef248SJed Brown PetscInt Xstate; 203a7a1495cSBarry Smith 204a7a1495cSBarry Smith PetscFunctionBegin; 2050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2060700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 207c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 2080e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 2090e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 2100e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2110e4ef248SJed Brown PetscFunctionReturn(0); 212f8ede8e7SJed Brown } 213d90be118SSean Farley 2143b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 215d90be118SSean Farley 2163b5f76d0SSean Farley if (ts->userops->rhsjacobian) { 217d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 218a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 219a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 2203b5f76d0SSean Farley ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 221a7a1495cSBarry Smith PetscStackPop; 222d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 223a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2240700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2250700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 226ef66eb69SBarry Smith } else { 227214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 228214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 229214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 230ef66eb69SBarry Smith } 2310e4ef248SJed Brown ts->rhsjacobian.time = t; 2320e4ef248SJed Brown ts->rhsjacobian.X = X; 2330e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2340e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 235a7a1495cSBarry Smith PetscFunctionReturn(0); 236a7a1495cSBarry Smith } 237a7a1495cSBarry Smith 2384a2ae208SSatish Balay #undef __FUNCT__ 2394a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 240316643e7SJed Brown /*@ 241d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 242d763cef2SBarry Smith 243316643e7SJed Brown Collective on TS and Vec 244316643e7SJed Brown 245316643e7SJed Brown Input Parameters: 246316643e7SJed Brown + ts - the TS context 247316643e7SJed Brown . t - current time 248316643e7SJed Brown - x - state vector 249316643e7SJed Brown 250316643e7SJed Brown Output Parameter: 251316643e7SJed Brown . y - right hand side 252316643e7SJed Brown 253316643e7SJed Brown Note: 254316643e7SJed Brown Most users should not need to explicitly call this routine, as it 255316643e7SJed Brown is used internally within the nonlinear solvers. 256316643e7SJed Brown 257316643e7SJed Brown Level: developer 258316643e7SJed Brown 259316643e7SJed Brown .keywords: TS, compute 260316643e7SJed Brown 261316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 262316643e7SJed Brown @*/ 263dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 264d763cef2SBarry Smith { 265dfbe8321SBarry Smith PetscErrorCode ierr; 266d763cef2SBarry Smith 267d763cef2SBarry Smith PetscFunctionBegin; 2680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2690700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2700700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 271d763cef2SBarry Smith 2723b5f76d0SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 273d763cef2SBarry Smith 274d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 2753b5f76d0SSean Farley if (ts->userops->rhsfunction) { 276d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 2773b5f76d0SSean Farley ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 278d763cef2SBarry Smith PetscStackPop; 279214bc6a2SJed Brown } else { 280214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 281b2cd27e8SJed Brown } 28244a41b28SSean Farley 283d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 284d763cef2SBarry Smith PetscFunctionReturn(0); 285d763cef2SBarry Smith } 286d763cef2SBarry Smith 2874a2ae208SSatish Balay #undef __FUNCT__ 288214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 289214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 290214bc6a2SJed Brown { 2912dd45cf8SJed Brown Vec F; 292214bc6a2SJed Brown PetscErrorCode ierr; 293214bc6a2SJed Brown 294214bc6a2SJed Brown PetscFunctionBegin; 2950da9a4f0SJed Brown *Frhs = PETSC_NULL; 2962dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 297214bc6a2SJed Brown if (!ts->Frhs) { 2982dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 299214bc6a2SJed Brown } 300214bc6a2SJed Brown *Frhs = ts->Frhs; 301214bc6a2SJed Brown PetscFunctionReturn(0); 302214bc6a2SJed Brown } 303214bc6a2SJed Brown 304214bc6a2SJed Brown #undef __FUNCT__ 305214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 306214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 307214bc6a2SJed Brown { 308214bc6a2SJed Brown Mat A,B; 3092dd45cf8SJed Brown PetscErrorCode ierr; 310214bc6a2SJed Brown 311214bc6a2SJed Brown PetscFunctionBegin; 312214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 313214bc6a2SJed Brown if (Arhs) { 314214bc6a2SJed Brown if (!ts->Arhs) { 315214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 316214bc6a2SJed Brown } 317214bc6a2SJed Brown *Arhs = ts->Arhs; 318214bc6a2SJed Brown } 319214bc6a2SJed Brown if (Brhs) { 320214bc6a2SJed Brown if (!ts->Brhs) { 321214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 322214bc6a2SJed Brown } 323214bc6a2SJed Brown *Brhs = ts->Brhs; 324214bc6a2SJed Brown } 325214bc6a2SJed Brown PetscFunctionReturn(0); 326214bc6a2SJed Brown } 327214bc6a2SJed Brown 328214bc6a2SJed Brown #undef __FUNCT__ 329316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 330316643e7SJed Brown /*@ 331316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 332316643e7SJed Brown 333316643e7SJed Brown Collective on TS and Vec 334316643e7SJed Brown 335316643e7SJed Brown Input Parameters: 336316643e7SJed Brown + ts - the TS context 337316643e7SJed Brown . t - current time 338316643e7SJed Brown . X - state vector 339214bc6a2SJed Brown . Xdot - time derivative of state vector 340214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 341316643e7SJed Brown 342316643e7SJed Brown Output Parameter: 343316643e7SJed Brown . Y - right hand side 344316643e7SJed Brown 345316643e7SJed Brown Note: 346316643e7SJed Brown Most users should not need to explicitly call this routine, as it 347316643e7SJed Brown is used internally within the nonlinear solvers. 348316643e7SJed Brown 349316643e7SJed Brown If the user did did not write their equations in implicit form, this 350316643e7SJed Brown function recasts them in implicit form. 351316643e7SJed Brown 352316643e7SJed Brown Level: developer 353316643e7SJed Brown 354316643e7SJed Brown .keywords: TS, compute 355316643e7SJed Brown 356316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 357316643e7SJed Brown @*/ 358214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 359316643e7SJed Brown { 360316643e7SJed Brown PetscErrorCode ierr; 361316643e7SJed Brown 362316643e7SJed Brown PetscFunctionBegin; 3630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3640700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3650700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3660700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 367316643e7SJed Brown 368d90be118SSean Farley if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 369d90be118SSean Farley 370316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 3713b5f76d0SSean Farley if (ts->userops->ifunction) { 372316643e7SJed Brown PetscStackPush("TS user implicit function"); 3733b5f76d0SSean Farley ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 374316643e7SJed Brown PetscStackPop; 375214bc6a2SJed Brown } 376214bc6a2SJed Brown if (imex) { 3773b5f76d0SSean Farley if (!ts->userops->ifunction) { 3782dd45cf8SJed Brown ierr = VecCopy(Xdot,Y);CHKERRQ(ierr); 3792dd45cf8SJed Brown } 3802dd45cf8SJed Brown } else if (ts->userops->rhsfunction) { 3812dd45cf8SJed Brown if (ts->userops->ifunction) { 382214bc6a2SJed Brown Vec Frhs; 383214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 384214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 385214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 3862dd45cf8SJed Brown } else { 3872dd45cf8SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 3882dd45cf8SJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 389316643e7SJed Brown } 3904a6899ffSJed Brown } 391316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 392316643e7SJed Brown PetscFunctionReturn(0); 393316643e7SJed Brown } 394316643e7SJed Brown 395316643e7SJed Brown #undef __FUNCT__ 396316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 397316643e7SJed Brown /*@ 398316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 399316643e7SJed Brown 400316643e7SJed Brown Collective on TS and Vec 401316643e7SJed Brown 402316643e7SJed Brown Input 403316643e7SJed Brown Input Parameters: 404316643e7SJed Brown + ts - the TS context 405316643e7SJed Brown . t - current timestep 406316643e7SJed Brown . X - state vector 407316643e7SJed Brown . Xdot - time derivative of state vector 408214bc6a2SJed Brown . shift - shift to apply, see note below 409214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 410316643e7SJed Brown 411316643e7SJed Brown Output Parameters: 412316643e7SJed Brown + A - Jacobian matrix 413316643e7SJed Brown . B - optional preconditioning matrix 414316643e7SJed Brown - flag - flag indicating matrix structure 415316643e7SJed Brown 416316643e7SJed Brown Notes: 417316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 418316643e7SJed Brown 419316643e7SJed Brown dF/dX + shift*dF/dXdot 420316643e7SJed Brown 421316643e7SJed Brown Most users should not need to explicitly call this routine, as it 422316643e7SJed Brown is used internally within the nonlinear solvers. 423316643e7SJed Brown 424316643e7SJed Brown Level: developer 425316643e7SJed Brown 426316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 427316643e7SJed Brown 428316643e7SJed Brown .seealso: TSSetIJacobian() 42963495f91SJed Brown @*/ 430214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 431316643e7SJed Brown { 4320026cea9SSean Farley PetscInt Xstate, Xdotstate; 433316643e7SJed Brown PetscErrorCode ierr; 434316643e7SJed Brown 435316643e7SJed Brown PetscFunctionBegin; 4360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4370700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4380700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 439316643e7SJed Brown PetscValidPointer(A,6); 4400700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 441316643e7SJed Brown PetscValidPointer(B,7); 4420700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 443316643e7SJed Brown PetscValidPointer(flg,8); 4440026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 4450026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 4460026cea9SSean 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))) { 4470026cea9SSean Farley *flg = ts->ijacobian.mstructure; 4480026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4490026cea9SSean Farley PetscFunctionReturn(0); 4500026cea9SSean Farley } 451316643e7SJed Brown 4523b5f76d0SSean Farley if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 453316643e7SJed Brown 4544e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 455316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 4563b5f76d0SSean Farley if (ts->userops->ijacobian) { 4572dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 458316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 4593b5f76d0SSean Farley ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 460316643e7SJed Brown PetscStackPop; 461214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 462214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 463214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 4644a6899ffSJed Brown } 465214bc6a2SJed Brown if (imex) { 4663b5f76d0SSean Farley if (!ts->userops->ijacobian) { /* system was written as Xdot = F(t,X) */ 467214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 4682dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 469214bc6a2SJed Brown if (*A != *B) { 470214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 471214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 472214bc6a2SJed Brown } 473214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 474214bc6a2SJed Brown } 475214bc6a2SJed Brown } else { 4763b5f76d0SSean Farley if (!ts->userops->ijacobian) { 477214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 478214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 479214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 480316643e7SJed Brown if (*A != *B) { 481316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 482316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 483316643e7SJed Brown } 4843b5f76d0SSean Farley } else if (ts->userops->rhsjacobian) { 485214bc6a2SJed Brown Mat Arhs,Brhs; 486214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 487214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 488214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 489214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 490214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 491214bc6a2SJed Brown if (*A != *B) { 492214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 493214bc6a2SJed Brown } 494214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 495214bc6a2SJed Brown } 496316643e7SJed Brown } 4970026cea9SSean Farley 4980026cea9SSean Farley ts->ijacobian.time = t; 4990026cea9SSean Farley ts->ijacobian.X = X; 5000026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 5010026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 5020026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 5030026cea9SSean Farley ts->ijacobian.shift = shift; 5040026cea9SSean Farley ts->ijacobian.imex = imex; 5050026cea9SSean Farley ts->ijacobian.mstructure = *flg; 506316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 507316643e7SJed Brown PetscFunctionReturn(0); 508316643e7SJed Brown } 509316643e7SJed Brown 510316643e7SJed Brown #undef __FUNCT__ 5114a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 512d763cef2SBarry Smith /*@C 513d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 514d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 515d763cef2SBarry Smith 5163f9fe445SBarry Smith Logically Collective on TS 517d763cef2SBarry Smith 518d763cef2SBarry Smith Input Parameters: 519d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 520ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 521d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 522d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 523d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 524d763cef2SBarry Smith 525d763cef2SBarry Smith Calling sequence of func: 52687828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 527d763cef2SBarry Smith 528d763cef2SBarry Smith + t - current timestep 529d763cef2SBarry Smith . u - input vector 530d763cef2SBarry Smith . F - function vector 531d763cef2SBarry Smith - ctx - [optional] user-defined function context 532d763cef2SBarry Smith 533d763cef2SBarry Smith Level: beginner 534d763cef2SBarry Smith 535d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 536d763cef2SBarry Smith 537*d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 538d763cef2SBarry Smith @*/ 539089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 540d763cef2SBarry Smith { 541089b2837SJed Brown PetscErrorCode ierr; 542089b2837SJed Brown SNES snes; 543d763cef2SBarry Smith 544089b2837SJed Brown PetscFunctionBegin; 5450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 546ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 5473b5f76d0SSean Farley if (f) ts->userops->rhsfunction = f; 5484e684422SJed Brown if (ctx) ts->funP = ctx; 549089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 550089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 551d763cef2SBarry Smith PetscFunctionReturn(0); 552d763cef2SBarry Smith } 553d763cef2SBarry Smith 5544a2ae208SSatish Balay #undef __FUNCT__ 5554a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 556d763cef2SBarry Smith /*@C 557d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 558d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 559d763cef2SBarry Smith 5603f9fe445SBarry Smith Logically Collective on TS 561d763cef2SBarry Smith 562d763cef2SBarry Smith Input Parameters: 563d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 564d763cef2SBarry Smith . A - Jacobian matrix 565d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 566d763cef2SBarry Smith . f - the Jacobian evaluation routine 567d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 568d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 569d763cef2SBarry Smith 570d763cef2SBarry Smith Calling sequence of func: 57187828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 572d763cef2SBarry Smith 573d763cef2SBarry Smith + t - current timestep 574d763cef2SBarry Smith . u - input vector 575d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 576d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 577d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 57894b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 579d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 580d763cef2SBarry Smith 581d763cef2SBarry Smith Notes: 58294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 583d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 584d763cef2SBarry Smith 585d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 586d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 58756335db2SHong Zhang completely new matrix structure (not just different matrix elements) 588d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 589d763cef2SBarry Smith throughout the global iterations. 590d763cef2SBarry Smith 591d763cef2SBarry Smith Level: beginner 592d763cef2SBarry Smith 593d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 594d763cef2SBarry Smith 595d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 596*d6cbdb99SBarry Smith SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 597d763cef2SBarry Smith 598d763cef2SBarry Smith @*/ 599089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 600d763cef2SBarry Smith { 601277b19d0SLisandro Dalcin PetscErrorCode ierr; 602089b2837SJed Brown SNES snes; 603277b19d0SLisandro Dalcin 604d763cef2SBarry Smith PetscFunctionBegin; 6050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 606277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 607277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 608277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 609277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 610d763cef2SBarry Smith 6113b5f76d0SSean Farley if (f) ts->userops->rhsjacobian = f; 612277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 613089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 614dabe61deSJed Brown if (!ts->userops->ijacobian) { 615089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 6160e4ef248SJed Brown } 6170e4ef248SJed Brown if (A) { 6180e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 6190e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 6200e4ef248SJed Brown ts->Arhs = A; 6210e4ef248SJed Brown } 6220e4ef248SJed Brown if (B) { 6230e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 6240e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 6250e4ef248SJed Brown ts->Brhs = B; 6260e4ef248SJed Brown } 627d763cef2SBarry Smith PetscFunctionReturn(0); 628d763cef2SBarry Smith } 629d763cef2SBarry Smith 630316643e7SJed Brown 631316643e7SJed Brown #undef __FUNCT__ 632316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 633316643e7SJed Brown /*@C 634316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 635316643e7SJed Brown 6363f9fe445SBarry Smith Logically Collective on TS 637316643e7SJed Brown 638316643e7SJed Brown Input Parameters: 639316643e7SJed Brown + ts - the TS context obtained from TSCreate() 640ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 641316643e7SJed Brown . f - the function evaluation routine 642316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 643316643e7SJed Brown 644316643e7SJed Brown Calling sequence of f: 645316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 646316643e7SJed Brown 647316643e7SJed Brown + t - time at step/stage being solved 648316643e7SJed Brown . u - state vector 649316643e7SJed Brown . u_t - time derivative of state vector 650316643e7SJed Brown . F - function vector 651316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 652316643e7SJed Brown 653316643e7SJed Brown Important: 654*d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 655316643e7SJed Brown 656316643e7SJed Brown Level: beginner 657316643e7SJed Brown 658316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 659316643e7SJed Brown 660*d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 661316643e7SJed Brown @*/ 662089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 663316643e7SJed Brown { 664089b2837SJed Brown PetscErrorCode ierr; 665089b2837SJed Brown SNES snes; 666316643e7SJed Brown 667316643e7SJed Brown PetscFunctionBegin; 6680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 669ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 6703b5f76d0SSean Farley if (f) ts->userops->ifunction = f; 671089b2837SJed Brown if (ctx) ts->funP = ctx; 672089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 673089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 674089b2837SJed Brown PetscFunctionReturn(0); 675089b2837SJed Brown } 676089b2837SJed Brown 677089b2837SJed Brown #undef __FUNCT__ 678089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 679089b2837SJed Brown /*@C 680089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 681089b2837SJed Brown 682089b2837SJed Brown Not Collective 683089b2837SJed Brown 684089b2837SJed Brown Input Parameter: 685089b2837SJed Brown . ts - the TS context 686089b2837SJed Brown 687089b2837SJed Brown Output Parameter: 688089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 689089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 690089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 691089b2837SJed Brown 692089b2837SJed Brown Level: advanced 693089b2837SJed Brown 694089b2837SJed Brown .keywords: TS, nonlinear, get, function 695089b2837SJed Brown 696089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 697089b2837SJed Brown @*/ 698089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 699089b2837SJed Brown { 700089b2837SJed Brown PetscErrorCode ierr; 701089b2837SJed Brown SNES snes; 702089b2837SJed Brown 703089b2837SJed Brown PetscFunctionBegin; 704089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 705089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 706089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7073b5f76d0SSean Farley if (func) *func = ts->userops->ifunction; 708089b2837SJed Brown if (ctx) *ctx = ts->funP; 709089b2837SJed Brown PetscFunctionReturn(0); 710089b2837SJed Brown } 711089b2837SJed Brown 712089b2837SJed Brown #undef __FUNCT__ 713089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 714089b2837SJed Brown /*@C 715089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 716089b2837SJed Brown 717089b2837SJed Brown Not Collective 718089b2837SJed Brown 719089b2837SJed Brown Input Parameter: 720089b2837SJed Brown . ts - the TS context 721089b2837SJed Brown 722089b2837SJed Brown Output Parameter: 723089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 724089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 725089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 726089b2837SJed Brown 727089b2837SJed Brown Level: advanced 728089b2837SJed Brown 729089b2837SJed Brown .keywords: TS, nonlinear, get, function 730089b2837SJed Brown 731089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 732089b2837SJed Brown @*/ 733089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 734089b2837SJed Brown { 735089b2837SJed Brown PetscErrorCode ierr; 736089b2837SJed Brown SNES snes; 737089b2837SJed Brown 738089b2837SJed Brown PetscFunctionBegin; 739089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 740089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 741089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7423b5f76d0SSean Farley if (func) *func = ts->userops->rhsfunction; 743089b2837SJed Brown if (ctx) *ctx = ts->funP; 744316643e7SJed Brown PetscFunctionReturn(0); 745316643e7SJed Brown } 746316643e7SJed Brown 747316643e7SJed Brown #undef __FUNCT__ 748316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 749316643e7SJed Brown /*@C 750a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 751a4f0a591SBarry Smith you provided with TSSetIFunction(). 752316643e7SJed Brown 7533f9fe445SBarry Smith Logically Collective on TS 754316643e7SJed Brown 755316643e7SJed Brown Input Parameters: 756316643e7SJed Brown + ts - the TS context obtained from TSCreate() 757316643e7SJed Brown . A - Jacobian matrix 758316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 759316643e7SJed Brown . f - the Jacobian evaluation routine 760316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 761316643e7SJed Brown 762316643e7SJed Brown Calling sequence of f: 7631b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 764316643e7SJed Brown 765316643e7SJed Brown + t - time at step/stage being solved 7661b4a444bSJed Brown . U - state vector 7671b4a444bSJed Brown . U_t - time derivative of state vector 768316643e7SJed Brown . a - shift 7691b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 770316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 771316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 772316643e7SJed Brown structure (same as flag in KSPSetOperators()) 773316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 774316643e7SJed Brown 775316643e7SJed Brown Notes: 776316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 777316643e7SJed Brown 778a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 779a4f0a591SBarry 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. 780a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 781a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 782a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 783a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 784a4f0a591SBarry Smith 785316643e7SJed Brown Level: beginner 786316643e7SJed Brown 787316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 788316643e7SJed Brown 789316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 790316643e7SJed Brown 791316643e7SJed Brown @*/ 7927087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 793316643e7SJed Brown { 794316643e7SJed Brown PetscErrorCode ierr; 795089b2837SJed Brown SNES snes; 796316643e7SJed Brown 797316643e7SJed Brown PetscFunctionBegin; 7980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7990700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 8000700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 801316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 802316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 8033b5f76d0SSean Farley if (f) ts->userops->ijacobian = f; 804316643e7SJed Brown if (ctx) ts->jacP = ctx; 805089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 806089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 807316643e7SJed Brown PetscFunctionReturn(0); 808316643e7SJed Brown } 809316643e7SJed Brown 8104a2ae208SSatish Balay #undef __FUNCT__ 8114a2ae208SSatish Balay #define __FUNCT__ "TSView" 8127e2c5f70SBarry Smith /*@C 813d763cef2SBarry Smith TSView - Prints the TS data structure. 814d763cef2SBarry Smith 8154c49b128SBarry Smith Collective on TS 816d763cef2SBarry Smith 817d763cef2SBarry Smith Input Parameters: 818d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 819d763cef2SBarry Smith - viewer - visualization context 820d763cef2SBarry Smith 821d763cef2SBarry Smith Options Database Key: 822d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 823d763cef2SBarry Smith 824d763cef2SBarry Smith Notes: 825d763cef2SBarry Smith The available visualization contexts include 826b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 827b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 828d763cef2SBarry Smith output where only the first processor opens 829d763cef2SBarry Smith the file. All other processors send their 830d763cef2SBarry Smith data to the first processor to print. 831d763cef2SBarry Smith 832d763cef2SBarry Smith The user can open an alternative visualization context with 833b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 834d763cef2SBarry Smith 835d763cef2SBarry Smith Level: beginner 836d763cef2SBarry Smith 837d763cef2SBarry Smith .keywords: TS, timestep, view 838d763cef2SBarry Smith 839b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 840d763cef2SBarry Smith @*/ 8417087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 842d763cef2SBarry Smith { 843dfbe8321SBarry Smith PetscErrorCode ierr; 844a313700dSBarry Smith const TSType type; 845089b2837SJed Brown PetscBool iascii,isstring,isundials; 846d763cef2SBarry Smith 847d763cef2SBarry Smith PetscFunctionBegin; 8480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8493050cee2SBarry Smith if (!viewer) { 8507adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 8513050cee2SBarry Smith } 8520700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 853c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 854fd16b177SBarry Smith 8552692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8562692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 85732077d6dSBarry Smith if (iascii) { 858317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 85977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 860a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 861d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 86277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 863c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 864d763cef2SBarry Smith } 86577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 866193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 867d52bd9f3SBarry Smith if (ts->ops->view) { 868d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 869d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 870d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 871d52bd9f3SBarry Smith } 8720f5bd95cSBarry Smith } else if (isstring) { 873a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 874b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 875d763cef2SBarry Smith } 876b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 877089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 878b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 879d763cef2SBarry Smith PetscFunctionReturn(0); 880d763cef2SBarry Smith } 881d763cef2SBarry Smith 882d763cef2SBarry Smith 8834a2ae208SSatish Balay #undef __FUNCT__ 8844a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 885b07ff414SBarry Smith /*@ 886d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 887d763cef2SBarry Smith the timesteppers. 888d763cef2SBarry Smith 8893f9fe445SBarry Smith Logically Collective on TS 890d763cef2SBarry Smith 891d763cef2SBarry Smith Input Parameters: 892d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 893d763cef2SBarry Smith - usrP - optional user context 894d763cef2SBarry Smith 895d763cef2SBarry Smith Level: intermediate 896d763cef2SBarry Smith 897d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 898d763cef2SBarry Smith 899d763cef2SBarry Smith .seealso: TSGetApplicationContext() 900d763cef2SBarry Smith @*/ 9017087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 902d763cef2SBarry Smith { 903d763cef2SBarry Smith PetscFunctionBegin; 9040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 905d763cef2SBarry Smith ts->user = usrP; 906d763cef2SBarry Smith PetscFunctionReturn(0); 907d763cef2SBarry Smith } 908d763cef2SBarry Smith 9094a2ae208SSatish Balay #undef __FUNCT__ 9104a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 911b07ff414SBarry Smith /*@ 912d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 913d763cef2SBarry Smith timestepper. 914d763cef2SBarry Smith 915d763cef2SBarry Smith Not Collective 916d763cef2SBarry Smith 917d763cef2SBarry Smith Input Parameter: 918d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 919d763cef2SBarry Smith 920d763cef2SBarry Smith Output Parameter: 921d763cef2SBarry Smith . usrP - user context 922d763cef2SBarry Smith 923d763cef2SBarry Smith Level: intermediate 924d763cef2SBarry Smith 925d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 926d763cef2SBarry Smith 927d763cef2SBarry Smith .seealso: TSSetApplicationContext() 928d763cef2SBarry Smith @*/ 929e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 930d763cef2SBarry Smith { 931d763cef2SBarry Smith PetscFunctionBegin; 9320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 933e71120c6SJed Brown *(void**)usrP = ts->user; 934d763cef2SBarry Smith PetscFunctionReturn(0); 935d763cef2SBarry Smith } 936d763cef2SBarry Smith 9374a2ae208SSatish Balay #undef __FUNCT__ 9384a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 939d763cef2SBarry Smith /*@ 940d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 941d763cef2SBarry Smith 942d763cef2SBarry Smith Not Collective 943d763cef2SBarry Smith 944d763cef2SBarry Smith Input Parameter: 945d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 946d763cef2SBarry Smith 947d763cef2SBarry Smith Output Parameter: 948d763cef2SBarry Smith . iter - number steps so far 949d763cef2SBarry Smith 950d763cef2SBarry Smith Level: intermediate 951d763cef2SBarry Smith 952d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 953d763cef2SBarry Smith @*/ 9547087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 955d763cef2SBarry Smith { 956d763cef2SBarry Smith PetscFunctionBegin; 9570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9584482741eSBarry Smith PetscValidIntPointer(iter,2); 959d763cef2SBarry Smith *iter = ts->steps; 960d763cef2SBarry Smith PetscFunctionReturn(0); 961d763cef2SBarry Smith } 962d763cef2SBarry Smith 9634a2ae208SSatish Balay #undef __FUNCT__ 9644a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 965d763cef2SBarry Smith /*@ 966d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 967d763cef2SBarry Smith as well as the initial time. 968d763cef2SBarry Smith 9693f9fe445SBarry Smith Logically Collective on TS 970d763cef2SBarry Smith 971d763cef2SBarry Smith Input Parameters: 972d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 973d763cef2SBarry Smith . initial_time - the initial time 974d763cef2SBarry Smith - time_step - the size of the timestep 975d763cef2SBarry Smith 976d763cef2SBarry Smith Level: intermediate 977d763cef2SBarry Smith 978d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 979d763cef2SBarry Smith 980d763cef2SBarry Smith .keywords: TS, set, initial, timestep 981d763cef2SBarry Smith @*/ 9827087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 983d763cef2SBarry Smith { 984e144a568SJed Brown PetscErrorCode ierr; 985e144a568SJed Brown 986d763cef2SBarry Smith PetscFunctionBegin; 9870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 988e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 989d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 990d763cef2SBarry Smith PetscFunctionReturn(0); 991d763cef2SBarry Smith } 992d763cef2SBarry Smith 9934a2ae208SSatish Balay #undef __FUNCT__ 9944a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 995d763cef2SBarry Smith /*@ 996d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 997d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 998d763cef2SBarry Smith 9993f9fe445SBarry Smith Logically Collective on TS 1000d763cef2SBarry Smith 1001d763cef2SBarry Smith Input Parameters: 1002d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1003d763cef2SBarry Smith - time_step - the size of the timestep 1004d763cef2SBarry Smith 1005d763cef2SBarry Smith Level: intermediate 1006d763cef2SBarry Smith 1007d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1008d763cef2SBarry Smith 1009d763cef2SBarry Smith .keywords: TS, set, timestep 1010d763cef2SBarry Smith @*/ 10117087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1012d763cef2SBarry Smith { 1013d763cef2SBarry Smith PetscFunctionBegin; 10140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1015c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1016d763cef2SBarry Smith ts->time_step = time_step; 1017d763cef2SBarry Smith PetscFunctionReturn(0); 1018d763cef2SBarry Smith } 1019d763cef2SBarry Smith 10204a2ae208SSatish Balay #undef __FUNCT__ 1021a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1022a43b19c4SJed Brown /*@ 1023a43b19c4SJed Brown TSSetExactFinalTime - Determines whether to interpolate solution to the 1024a43b19c4SJed Brown exact final time requested by the user or just returns it at the final time 1025a43b19c4SJed Brown it computed. 1026a43b19c4SJed Brown 1027a43b19c4SJed Brown Logically Collective on TS 1028a43b19c4SJed Brown 1029a43b19c4SJed Brown Input Parameter: 1030a43b19c4SJed Brown + ts - the time-step context 1031a43b19c4SJed Brown - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1032a43b19c4SJed Brown 1033a43b19c4SJed Brown Level: beginner 1034a43b19c4SJed Brown 1035a43b19c4SJed Brown .seealso: TSSetDuration() 1036a43b19c4SJed Brown @*/ 1037a43b19c4SJed Brown PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1038a43b19c4SJed Brown { 1039a43b19c4SJed Brown 1040a43b19c4SJed Brown PetscFunctionBegin; 1041a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1042d8cd7023SBarry Smith PetscValidLogicalCollectiveBool(ts,flg,2); 1043a43b19c4SJed Brown ts->exact_final_time = flg; 1044a43b19c4SJed Brown PetscFunctionReturn(0); 1045a43b19c4SJed Brown } 1046a43b19c4SJed Brown 1047a43b19c4SJed Brown #undef __FUNCT__ 10484a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1049d763cef2SBarry Smith /*@ 1050d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1051d763cef2SBarry Smith 1052d763cef2SBarry Smith Not Collective 1053d763cef2SBarry Smith 1054d763cef2SBarry Smith Input Parameter: 1055d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1056d763cef2SBarry Smith 1057d763cef2SBarry Smith Output Parameter: 1058d763cef2SBarry Smith . dt - the current timestep size 1059d763cef2SBarry Smith 1060d763cef2SBarry Smith Level: intermediate 1061d763cef2SBarry Smith 1062d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1063d763cef2SBarry Smith 1064d763cef2SBarry Smith .keywords: TS, get, timestep 1065d763cef2SBarry Smith @*/ 10667087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1067d763cef2SBarry Smith { 1068d763cef2SBarry Smith PetscFunctionBegin; 10690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10704482741eSBarry Smith PetscValidDoublePointer(dt,2); 1071d763cef2SBarry Smith *dt = ts->time_step; 1072d763cef2SBarry Smith PetscFunctionReturn(0); 1073d763cef2SBarry Smith } 1074d763cef2SBarry Smith 10754a2ae208SSatish Balay #undef __FUNCT__ 10764a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1077d8e5e3e6SSatish Balay /*@ 1078d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1079d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1080d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1081d763cef2SBarry Smith the solution at the next timestep has been calculated. 1082d763cef2SBarry Smith 1083d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1084d763cef2SBarry Smith 1085d763cef2SBarry Smith Input Parameter: 1086d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1087d763cef2SBarry Smith 1088d763cef2SBarry Smith Output Parameter: 1089d763cef2SBarry Smith . v - the vector containing the solution 1090d763cef2SBarry Smith 1091d763cef2SBarry Smith Level: intermediate 1092d763cef2SBarry Smith 1093d763cef2SBarry Smith .seealso: TSGetTimeStep() 1094d763cef2SBarry Smith 1095d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1096d763cef2SBarry Smith @*/ 10977087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1098d763cef2SBarry Smith { 1099d763cef2SBarry Smith PetscFunctionBegin; 11000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11014482741eSBarry Smith PetscValidPointer(v,2); 11028737fe31SLisandro Dalcin *v = ts->vec_sol; 1103d763cef2SBarry Smith PetscFunctionReturn(0); 1104d763cef2SBarry Smith } 1105d763cef2SBarry Smith 1106bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 11074a2ae208SSatish Balay #undef __FUNCT__ 1108bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1109d8e5e3e6SSatish Balay /*@ 1110bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1111d763cef2SBarry Smith 1112bdad233fSMatthew Knepley Not collective 1113d763cef2SBarry Smith 1114bdad233fSMatthew Knepley Input Parameters: 1115bdad233fSMatthew Knepley + ts - The TS 1116bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1117d763cef2SBarry Smith .vb 1118d763cef2SBarry Smith U_t = A U 1119d763cef2SBarry Smith U_t = A(t) U 1120d763cef2SBarry Smith U_t = F(t,U) 1121d763cef2SBarry Smith .ve 1122d763cef2SBarry Smith 1123d763cef2SBarry Smith Level: beginner 1124d763cef2SBarry Smith 1125bdad233fSMatthew Knepley .keywords: TS, problem type 1126bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1127d763cef2SBarry Smith @*/ 11287087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1129a7cc72afSBarry Smith { 11309e2a6581SJed Brown PetscErrorCode ierr; 11319e2a6581SJed Brown 1132d763cef2SBarry Smith PetscFunctionBegin; 11330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1134bdad233fSMatthew Knepley ts->problem_type = type; 11359e2a6581SJed Brown if (type == TS_LINEAR) { 11369e2a6581SJed Brown SNES snes; 11379e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11389e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 11399e2a6581SJed Brown } 1140d763cef2SBarry Smith PetscFunctionReturn(0); 1141d763cef2SBarry Smith } 1142d763cef2SBarry Smith 1143bdad233fSMatthew Knepley #undef __FUNCT__ 1144bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1145bdad233fSMatthew Knepley /*@C 1146bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1147bdad233fSMatthew Knepley 1148bdad233fSMatthew Knepley Not collective 1149bdad233fSMatthew Knepley 1150bdad233fSMatthew Knepley Input Parameter: 1151bdad233fSMatthew Knepley . ts - The TS 1152bdad233fSMatthew Knepley 1153bdad233fSMatthew Knepley Output Parameter: 1154bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1155bdad233fSMatthew Knepley .vb 1156089b2837SJed Brown M U_t = A U 1157089b2837SJed Brown M(t) U_t = A(t) U 1158bdad233fSMatthew Knepley U_t = F(t,U) 1159bdad233fSMatthew Knepley .ve 1160bdad233fSMatthew Knepley 1161bdad233fSMatthew Knepley Level: beginner 1162bdad233fSMatthew Knepley 1163bdad233fSMatthew Knepley .keywords: TS, problem type 1164bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1165bdad233fSMatthew Knepley @*/ 11667087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1167a7cc72afSBarry Smith { 1168bdad233fSMatthew Knepley PetscFunctionBegin; 11690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11704482741eSBarry Smith PetscValidIntPointer(type,2); 1171bdad233fSMatthew Knepley *type = ts->problem_type; 1172bdad233fSMatthew Knepley PetscFunctionReturn(0); 1173bdad233fSMatthew Knepley } 1174d763cef2SBarry Smith 11754a2ae208SSatish Balay #undef __FUNCT__ 11764a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1177d763cef2SBarry Smith /*@ 1178d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1179d763cef2SBarry Smith of a timestepper. 1180d763cef2SBarry Smith 1181d763cef2SBarry Smith Collective on TS 1182d763cef2SBarry Smith 1183d763cef2SBarry Smith Input Parameter: 1184d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1185d763cef2SBarry Smith 1186d763cef2SBarry Smith Notes: 1187d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1188d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1189d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1190d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1191d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1192d763cef2SBarry Smith 1193d763cef2SBarry Smith Level: advanced 1194d763cef2SBarry Smith 1195d763cef2SBarry Smith .keywords: TS, timestep, setup 1196d763cef2SBarry Smith 1197d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1198d763cef2SBarry Smith @*/ 11997087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1200d763cef2SBarry Smith { 1201dfbe8321SBarry Smith PetscErrorCode ierr; 1202d763cef2SBarry Smith 1203d763cef2SBarry Smith PetscFunctionBegin; 12040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1205277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1206277b19d0SLisandro Dalcin 12077adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 12089596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1209d763cef2SBarry Smith } 1210a43b19c4SJed Brown if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1211277b19d0SLisandro Dalcin 1212277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1213277b19d0SLisandro Dalcin 12141c3436cfSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 12151c3436cfSJed Brown 1216277b19d0SLisandro Dalcin if (ts->ops->setup) { 1217000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1218277b19d0SLisandro Dalcin } 1219277b19d0SLisandro Dalcin 1220277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1221277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1222277b19d0SLisandro Dalcin } 1223277b19d0SLisandro Dalcin 1224277b19d0SLisandro Dalcin #undef __FUNCT__ 1225277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1226277b19d0SLisandro Dalcin /*@ 1227277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1228277b19d0SLisandro Dalcin 1229277b19d0SLisandro Dalcin Collective on TS 1230277b19d0SLisandro Dalcin 1231277b19d0SLisandro Dalcin Input Parameter: 1232277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1233277b19d0SLisandro Dalcin 1234277b19d0SLisandro Dalcin Level: beginner 1235277b19d0SLisandro Dalcin 1236277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1237277b19d0SLisandro Dalcin 1238277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1239277b19d0SLisandro Dalcin @*/ 1240277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1241277b19d0SLisandro Dalcin { 1242277b19d0SLisandro Dalcin PetscErrorCode ierr; 1243277b19d0SLisandro Dalcin 1244277b19d0SLisandro Dalcin PetscFunctionBegin; 1245277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1246277b19d0SLisandro Dalcin if (ts->ops->reset) { 1247277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1248277b19d0SLisandro Dalcin } 1249277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12504e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 12514e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1252214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 12536bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 125438637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1255277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1256d763cef2SBarry Smith PetscFunctionReturn(0); 1257d763cef2SBarry Smith } 1258d763cef2SBarry Smith 12594a2ae208SSatish Balay #undef __FUNCT__ 12604a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1261d8e5e3e6SSatish Balay /*@ 1262d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1263d763cef2SBarry Smith with TSCreate(). 1264d763cef2SBarry Smith 1265d763cef2SBarry Smith Collective on TS 1266d763cef2SBarry Smith 1267d763cef2SBarry Smith Input Parameter: 1268d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1269d763cef2SBarry Smith 1270d763cef2SBarry Smith Level: beginner 1271d763cef2SBarry Smith 1272d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1273d763cef2SBarry Smith 1274d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1275d763cef2SBarry Smith @*/ 12766bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1277d763cef2SBarry Smith { 12786849ba73SBarry Smith PetscErrorCode ierr; 1279d763cef2SBarry Smith 1280d763cef2SBarry Smith PetscFunctionBegin; 12816bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12826bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12836bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1284d763cef2SBarry Smith 12856bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1286277b19d0SLisandro Dalcin 1287be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12886bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 12896bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 12906d4c513bSLisandro Dalcin 129184df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 12926bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 12936bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 12946bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 12956d4c513bSLisandro Dalcin 12963b5f76d0SSean Farley ierr = PetscFree((*ts)->userops); 12973b5f76d0SSean Farley 1298a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1299d763cef2SBarry Smith PetscFunctionReturn(0); 1300d763cef2SBarry Smith } 1301d763cef2SBarry Smith 13024a2ae208SSatish Balay #undef __FUNCT__ 13034a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1304d8e5e3e6SSatish Balay /*@ 1305d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1306d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1307d763cef2SBarry Smith 1308d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1309d763cef2SBarry Smith 1310d763cef2SBarry Smith Input Parameter: 1311d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1312d763cef2SBarry Smith 1313d763cef2SBarry Smith Output Parameter: 1314d763cef2SBarry Smith . snes - the nonlinear solver context 1315d763cef2SBarry Smith 1316d763cef2SBarry Smith Notes: 1317d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1318d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 131994b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1320d763cef2SBarry Smith 1321d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1322d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1323d763cef2SBarry Smith 1324d763cef2SBarry Smith Level: beginner 1325d763cef2SBarry Smith 1326d763cef2SBarry Smith .keywords: timestep, get, SNES 1327d763cef2SBarry Smith @*/ 13287087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1329d763cef2SBarry Smith { 1330d372ba47SLisandro Dalcin PetscErrorCode ierr; 1331d372ba47SLisandro Dalcin 1332d763cef2SBarry Smith PetscFunctionBegin; 13330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13344482741eSBarry Smith PetscValidPointer(snes,2); 1335d372ba47SLisandro Dalcin if (!ts->snes) { 1336d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1337d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1338d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 13399e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 13409e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 13419e2a6581SJed Brown } 1342d372ba47SLisandro Dalcin } 1343d763cef2SBarry Smith *snes = ts->snes; 1344d763cef2SBarry Smith PetscFunctionReturn(0); 1345d763cef2SBarry Smith } 1346d763cef2SBarry Smith 13474a2ae208SSatish Balay #undef __FUNCT__ 134894b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1349d8e5e3e6SSatish Balay /*@ 135094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1351d763cef2SBarry Smith a TS (timestepper) context. 1352d763cef2SBarry Smith 135394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1354d763cef2SBarry Smith 1355d763cef2SBarry Smith Input Parameter: 1356d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1357d763cef2SBarry Smith 1358d763cef2SBarry Smith Output Parameter: 135994b7f48cSBarry Smith . ksp - the nonlinear solver context 1360d763cef2SBarry Smith 1361d763cef2SBarry Smith Notes: 136294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1363d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1364d763cef2SBarry Smith KSP and PC contexts as well. 1365d763cef2SBarry Smith 136694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 136794b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1368d763cef2SBarry Smith 1369d763cef2SBarry Smith Level: beginner 1370d763cef2SBarry Smith 137194b7f48cSBarry Smith .keywords: timestep, get, KSP 1372d763cef2SBarry Smith @*/ 13737087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1374d763cef2SBarry Smith { 1375d372ba47SLisandro Dalcin PetscErrorCode ierr; 1376089b2837SJed Brown SNES snes; 1377d372ba47SLisandro Dalcin 1378d763cef2SBarry Smith PetscFunctionBegin; 13790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13804482741eSBarry Smith PetscValidPointer(ksp,2); 138117186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1382e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1383089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1384089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1385d763cef2SBarry Smith PetscFunctionReturn(0); 1386d763cef2SBarry Smith } 1387d763cef2SBarry Smith 1388d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1389d763cef2SBarry Smith 13904a2ae208SSatish Balay #undef __FUNCT__ 1391adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1392adb62b0dSMatthew Knepley /*@ 1393adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1394adb62b0dSMatthew Knepley maximum time for iteration. 1395adb62b0dSMatthew Knepley 13963f9fe445SBarry Smith Not Collective 1397adb62b0dSMatthew Knepley 1398adb62b0dSMatthew Knepley Input Parameters: 1399adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1400adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1401adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1402adb62b0dSMatthew Knepley 1403adb62b0dSMatthew Knepley Level: intermediate 1404adb62b0dSMatthew Knepley 1405adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1406adb62b0dSMatthew Knepley @*/ 14077087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1408adb62b0dSMatthew Knepley { 1409adb62b0dSMatthew Knepley PetscFunctionBegin; 14100700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1411abc0a331SBarry Smith if (maxsteps) { 14124482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1413adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1414adb62b0dSMatthew Knepley } 1415abc0a331SBarry Smith if (maxtime) { 14164482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1417adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1418adb62b0dSMatthew Knepley } 1419adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1420adb62b0dSMatthew Knepley } 1421adb62b0dSMatthew Knepley 1422adb62b0dSMatthew Knepley #undef __FUNCT__ 14234a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1424d763cef2SBarry Smith /*@ 1425d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1426d763cef2SBarry Smith maximum time for iteration. 1427d763cef2SBarry Smith 14283f9fe445SBarry Smith Logically Collective on TS 1429d763cef2SBarry Smith 1430d763cef2SBarry Smith Input Parameters: 1431d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1432d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1433d763cef2SBarry Smith - maxtime - final time to iterate to 1434d763cef2SBarry Smith 1435d763cef2SBarry Smith Options Database Keys: 1436d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 14373bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1438d763cef2SBarry Smith 1439d763cef2SBarry Smith Notes: 1440d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1441d763cef2SBarry Smith 1442d763cef2SBarry Smith Level: intermediate 1443d763cef2SBarry Smith 1444d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1445a43b19c4SJed Brown 1446a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1447d763cef2SBarry Smith @*/ 14487087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1449d763cef2SBarry Smith { 1450d763cef2SBarry Smith PetscFunctionBegin; 14510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1452c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1453c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 145439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 145539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1456d763cef2SBarry Smith PetscFunctionReturn(0); 1457d763cef2SBarry Smith } 1458d763cef2SBarry Smith 14594a2ae208SSatish Balay #undef __FUNCT__ 14604a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1461d763cef2SBarry Smith /*@ 1462d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1463d763cef2SBarry Smith for use by the TS routines. 1464d763cef2SBarry Smith 14653f9fe445SBarry Smith Logically Collective on TS and Vec 1466d763cef2SBarry Smith 1467d763cef2SBarry Smith Input Parameters: 1468d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1469d763cef2SBarry Smith - x - the solution vector 1470d763cef2SBarry Smith 1471d763cef2SBarry Smith Level: beginner 1472d763cef2SBarry Smith 1473d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1474d763cef2SBarry Smith @*/ 14757087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1476d763cef2SBarry Smith { 14778737fe31SLisandro Dalcin PetscErrorCode ierr; 14788737fe31SLisandro Dalcin 1479d763cef2SBarry Smith PetscFunctionBegin; 14800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14810700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14828737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14836bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14848737fe31SLisandro Dalcin ts->vec_sol = x; 1485d763cef2SBarry Smith PetscFunctionReturn(0); 1486d763cef2SBarry Smith } 1487d763cef2SBarry Smith 1488e74ef692SMatthew Knepley #undef __FUNCT__ 1489e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1490ac226902SBarry Smith /*@C 1491000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 14923f2090d5SJed Brown called once at the beginning of each time step. 1493000e7ae3SMatthew Knepley 14943f9fe445SBarry Smith Logically Collective on TS 1495000e7ae3SMatthew Knepley 1496000e7ae3SMatthew Knepley Input Parameters: 1497000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1498000e7ae3SMatthew Knepley - func - The function 1499000e7ae3SMatthew Knepley 1500000e7ae3SMatthew Knepley Calling sequence of func: 1501000e7ae3SMatthew Knepley . func (TS ts); 1502000e7ae3SMatthew Knepley 1503000e7ae3SMatthew Knepley Level: intermediate 1504000e7ae3SMatthew Knepley 1505000e7ae3SMatthew Knepley .keywords: TS, timestep 1506000e7ae3SMatthew Knepley @*/ 15077087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1508000e7ae3SMatthew Knepley { 1509000e7ae3SMatthew Knepley PetscFunctionBegin; 15100700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1511000e7ae3SMatthew Knepley ts->ops->prestep = func; 1512000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1513000e7ae3SMatthew Knepley } 1514000e7ae3SMatthew Knepley 1515e74ef692SMatthew Knepley #undef __FUNCT__ 15163f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 151709ee8438SJed Brown /*@ 15183f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 15193f2090d5SJed Brown 15203f2090d5SJed Brown Collective on TS 15213f2090d5SJed Brown 15223f2090d5SJed Brown Input Parameters: 15233f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15243f2090d5SJed Brown 15253f2090d5SJed Brown Notes: 15263f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 15273f2090d5SJed Brown so most users would not generally call this routine themselves. 15283f2090d5SJed Brown 15293f2090d5SJed Brown Level: developer 15303f2090d5SJed Brown 15313f2090d5SJed Brown .keywords: TS, timestep 15323f2090d5SJed Brown @*/ 15337087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 15343f2090d5SJed Brown { 15353f2090d5SJed Brown PetscErrorCode ierr; 15363f2090d5SJed Brown 15373f2090d5SJed Brown PetscFunctionBegin; 15380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 153972ac3e02SJed Brown if (ts->ops->prestep) { 15403f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15413f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15423f2090d5SJed Brown PetscStackPop; 1543312ce896SJed Brown } 15443f2090d5SJed Brown PetscFunctionReturn(0); 15453f2090d5SJed Brown } 15463f2090d5SJed Brown 15473f2090d5SJed Brown #undef __FUNCT__ 1548e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1549ac226902SBarry Smith /*@C 1550000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15513f2090d5SJed Brown called once at the end of each time step. 1552000e7ae3SMatthew Knepley 15533f9fe445SBarry Smith Logically Collective on TS 1554000e7ae3SMatthew Knepley 1555000e7ae3SMatthew Knepley Input Parameters: 1556000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1557000e7ae3SMatthew Knepley - func - The function 1558000e7ae3SMatthew Knepley 1559000e7ae3SMatthew Knepley Calling sequence of func: 1560000e7ae3SMatthew Knepley . func (TS ts); 1561000e7ae3SMatthew Knepley 1562000e7ae3SMatthew Knepley Level: intermediate 1563000e7ae3SMatthew Knepley 1564000e7ae3SMatthew Knepley .keywords: TS, timestep 1565000e7ae3SMatthew Knepley @*/ 15667087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1567000e7ae3SMatthew Knepley { 1568000e7ae3SMatthew Knepley PetscFunctionBegin; 15690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1570000e7ae3SMatthew Knepley ts->ops->poststep = func; 1571000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1572000e7ae3SMatthew Knepley } 1573000e7ae3SMatthew Knepley 1574e74ef692SMatthew Knepley #undef __FUNCT__ 15753f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 157609ee8438SJed Brown /*@ 15773f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15783f2090d5SJed Brown 15793f2090d5SJed Brown Collective on TS 15803f2090d5SJed Brown 15813f2090d5SJed Brown Input Parameters: 15823f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15833f2090d5SJed Brown 15843f2090d5SJed Brown Notes: 15853f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15863f2090d5SJed Brown so most users would not generally call this routine themselves. 15873f2090d5SJed Brown 15883f2090d5SJed Brown Level: developer 15893f2090d5SJed Brown 15903f2090d5SJed Brown .keywords: TS, timestep 15913f2090d5SJed Brown @*/ 15927087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 15933f2090d5SJed Brown { 15943f2090d5SJed Brown PetscErrorCode ierr; 15953f2090d5SJed Brown 15963f2090d5SJed Brown PetscFunctionBegin; 15970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 159872ac3e02SJed Brown if (ts->ops->poststep) { 15993f2090d5SJed Brown PetscStackPush("TS PostStep function"); 16003f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 16013f2090d5SJed Brown PetscStackPop; 160272ac3e02SJed Brown } 16033f2090d5SJed Brown PetscFunctionReturn(0); 16043f2090d5SJed Brown } 16053f2090d5SJed Brown 1606d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1607d763cef2SBarry Smith 16084a2ae208SSatish Balay #undef __FUNCT__ 1609a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1610d763cef2SBarry Smith /*@C 1611a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1612d763cef2SBarry Smith timestep to display the iteration's progress. 1613d763cef2SBarry Smith 16143f9fe445SBarry Smith Logically Collective on TS 1615d763cef2SBarry Smith 1616d763cef2SBarry Smith Input Parameters: 1617d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1618e213d8f1SJed Brown . monitor - monitoring routine 1619329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1620b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1621b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1622b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1623d763cef2SBarry Smith 1624e213d8f1SJed Brown Calling sequence of monitor: 1625e213d8f1SJed Brown $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1626d763cef2SBarry Smith 1627d763cef2SBarry Smith + ts - the TS context 1628d763cef2SBarry Smith . steps - iteration number 16291f06c33eSBarry Smith . time - current time 1630d763cef2SBarry Smith . x - current iterate 1631d763cef2SBarry Smith - mctx - [optional] monitoring context 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith Notes: 1634d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1635d763cef2SBarry Smith already has been loaded. 1636d763cef2SBarry Smith 1637025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1638025f1a04SBarry Smith 1639d763cef2SBarry Smith Level: intermediate 1640d763cef2SBarry Smith 1641d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1642d763cef2SBarry Smith 1643a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1644d763cef2SBarry Smith @*/ 1645c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1646d763cef2SBarry Smith { 1647d763cef2SBarry Smith PetscFunctionBegin; 16480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 164917186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1650d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1651329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1652d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1653d763cef2SBarry Smith PetscFunctionReturn(0); 1654d763cef2SBarry Smith } 1655d763cef2SBarry Smith 16564a2ae208SSatish Balay #undef __FUNCT__ 1657a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1658d763cef2SBarry Smith /*@C 1659a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1660d763cef2SBarry Smith 16613f9fe445SBarry Smith Logically Collective on TS 1662d763cef2SBarry Smith 1663d763cef2SBarry Smith Input Parameters: 1664d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith Notes: 1667d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1668d763cef2SBarry Smith 1669d763cef2SBarry Smith Level: intermediate 1670d763cef2SBarry Smith 1671d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1672d763cef2SBarry Smith 1673a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1674d763cef2SBarry Smith @*/ 16757087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1676d763cef2SBarry Smith { 1677d952e501SBarry Smith PetscErrorCode ierr; 1678d952e501SBarry Smith PetscInt i; 1679d952e501SBarry Smith 1680d763cef2SBarry Smith PetscFunctionBegin; 16810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1682d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1683d952e501SBarry Smith if (ts->mdestroy[i]) { 16843c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1685d952e501SBarry Smith } 1686d952e501SBarry Smith } 1687d763cef2SBarry Smith ts->numbermonitors = 0; 1688d763cef2SBarry Smith PetscFunctionReturn(0); 1689d763cef2SBarry Smith } 1690d763cef2SBarry Smith 16914a2ae208SSatish Balay #undef __FUNCT__ 1692a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1693d8e5e3e6SSatish Balay /*@ 1694a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 16955516499fSSatish Balay 16965516499fSSatish Balay Level: intermediate 169741251cbbSSatish Balay 16985516499fSSatish Balay .keywords: TS, set, monitor 16995516499fSSatish Balay 170041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 170141251cbbSSatish Balay @*/ 1702649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1703d763cef2SBarry Smith { 1704dfbe8321SBarry Smith PetscErrorCode ierr; 1705649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1706d132466eSBarry Smith 1707d763cef2SBarry Smith PetscFunctionBegin; 1708649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1709971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 1710649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1711d763cef2SBarry Smith PetscFunctionReturn(0); 1712d763cef2SBarry Smith } 1713d763cef2SBarry Smith 17144a2ae208SSatish Balay #undef __FUNCT__ 1715cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 1716cd652676SJed Brown /*@ 1717cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 1718cd652676SJed Brown 1719cd652676SJed Brown Logically Collective on TS 1720cd652676SJed Brown 1721cd652676SJed Brown Input Argument: 1722cd652676SJed Brown . ts - time stepping context 1723cd652676SJed Brown 1724cd652676SJed Brown Output Argument: 1725cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 1726cd652676SJed Brown 1727cd652676SJed Brown Level: intermediate 1728cd652676SJed Brown 1729cd652676SJed Brown .keywords: TS, set 1730cd652676SJed Brown 1731cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 1732cd652676SJed Brown @*/ 1733cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 1734cd652676SJed Brown { 1735cd652676SJed Brown 1736cd652676SJed Brown PetscFunctionBegin; 1737cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1738cd652676SJed Brown ts->retain_stages = flg; 1739cd652676SJed Brown PetscFunctionReturn(0); 1740cd652676SJed Brown } 1741cd652676SJed Brown 1742cd652676SJed Brown #undef __FUNCT__ 1743cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 1744cd652676SJed Brown /*@ 1745cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 1746cd652676SJed Brown 1747cd652676SJed Brown Collective on TS 1748cd652676SJed Brown 1749cd652676SJed Brown Input Argument: 1750cd652676SJed Brown + ts - time stepping context 1751cd652676SJed Brown - t - time to interpolate to 1752cd652676SJed Brown 1753cd652676SJed Brown Output Argument: 1754cd652676SJed Brown . X - state at given time 1755cd652676SJed Brown 1756cd652676SJed Brown Notes: 1757cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 1758cd652676SJed Brown 1759cd652676SJed Brown Level: intermediate 1760cd652676SJed Brown 1761cd652676SJed Brown Developer Notes: 1762cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 1763cd652676SJed Brown 1764cd652676SJed Brown .keywords: TS, set 1765cd652676SJed Brown 1766cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 1767cd652676SJed Brown @*/ 1768cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 1769cd652676SJed Brown { 1770cd652676SJed Brown PetscErrorCode ierr; 1771cd652676SJed Brown 1772cd652676SJed Brown PetscFunctionBegin; 1773cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1774d8cd7023SBarry 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); 1775cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 1776cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 1777cd652676SJed Brown PetscFunctionReturn(0); 1778cd652676SJed Brown } 1779cd652676SJed Brown 1780cd652676SJed Brown #undef __FUNCT__ 17814a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1782d763cef2SBarry Smith /*@ 17836d9e5789SSean Farley TSStep - Steps one time step 1784d763cef2SBarry Smith 1785d763cef2SBarry Smith Collective on TS 1786d763cef2SBarry Smith 1787d763cef2SBarry Smith Input Parameter: 1788d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1789d763cef2SBarry Smith 17906d9e5789SSean Farley Level: intermediate 1791d763cef2SBarry Smith 1792d763cef2SBarry Smith .keywords: TS, timestep, solve 1793d763cef2SBarry Smith 17946d9e5789SSean Farley .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve() 1795d763cef2SBarry Smith @*/ 1796193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 1797d763cef2SBarry Smith { 1798362cd11cSLisandro Dalcin PetscReal ptime_prev; 1799dfbe8321SBarry Smith PetscErrorCode ierr; 1800d763cef2SBarry Smith 1801d763cef2SBarry Smith PetscFunctionBegin; 18020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1803d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1804d405a339SMatthew Knepley 1805362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 1806362cd11cSLisandro Dalcin 1807362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 1808d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 1809193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 1810d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 1811362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 1812362cd11cSLisandro Dalcin 1813362cd11cSLisandro Dalcin if (ts->reason < 0) { 1814f1b97656SJed Brown if (ts->errorifstepfailed) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 1815362cd11cSLisandro Dalcin } else if (!ts->reason) { 1816362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 1817362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 1818362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 1819362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 1820362cd11cSLisandro Dalcin } 1821362cd11cSLisandro Dalcin 1822d763cef2SBarry Smith PetscFunctionReturn(0); 1823d763cef2SBarry Smith } 1824d763cef2SBarry Smith 18254a2ae208SSatish Balay #undef __FUNCT__ 182605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 182705175c85SJed Brown /*@ 182805175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 182905175c85SJed Brown 18301c3436cfSJed Brown Collective on TS 183105175c85SJed Brown 183205175c85SJed Brown Input Arguments: 18331c3436cfSJed Brown + ts - time stepping context 18341c3436cfSJed Brown . order - desired order of accuracy 18351c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 183605175c85SJed Brown 183705175c85SJed Brown Output Arguments: 18381c3436cfSJed Brown . X - state at the end of the current step 183905175c85SJed Brown 184005175c85SJed Brown Level: advanced 184105175c85SJed Brown 1842108c343cSJed Brown Notes: 1843108c343cSJed Brown This function cannot be called until all stages have been evaluated. 1844108c343cSJed Brown It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after TSStep() has returned. 1845108c343cSJed Brown 18461c3436cfSJed Brown .seealso: TSStep(), TSAdapt 184705175c85SJed Brown @*/ 18481c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done) 184905175c85SJed Brown { 185005175c85SJed Brown PetscErrorCode ierr; 185105175c85SJed Brown 185205175c85SJed Brown PetscFunctionBegin; 185305175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 185405175c85SJed Brown PetscValidType(ts,1); 185505175c85SJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,3); 18561c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 18571c3436cfSJed Brown ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr); 185805175c85SJed Brown PetscFunctionReturn(0); 185905175c85SJed Brown } 186005175c85SJed Brown 186105175c85SJed Brown #undef __FUNCT__ 18626a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 18636a4d4014SLisandro Dalcin /*@ 18646a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 18656a4d4014SLisandro Dalcin 18666a4d4014SLisandro Dalcin Collective on TS 18676a4d4014SLisandro Dalcin 18686a4d4014SLisandro Dalcin Input Parameter: 18696a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 1870362cd11cSLisandro Dalcin - x - the solution vector 18716a4d4014SLisandro Dalcin 18725a3a76d0SJed Brown Output Parameter: 18735a3a76d0SJed Brown . ftime - time of the state vector x upon completion 18745a3a76d0SJed Brown 18756a4d4014SLisandro Dalcin Level: beginner 18766a4d4014SLisandro Dalcin 18775a3a76d0SJed Brown Notes: 18785a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 18795a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 18805a3a76d0SJed Brown stepped over the final time. 18815a3a76d0SJed Brown 18826a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 18836a4d4014SLisandro Dalcin 18846a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 18856a4d4014SLisandro Dalcin @*/ 18865a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime) 18876a4d4014SLisandro Dalcin { 18884d7d938eSLisandro Dalcin PetscBool flg; 18894d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 18904d7d938eSLisandro Dalcin PetscViewer viewer; 18916a4d4014SLisandro Dalcin PetscErrorCode ierr; 1892f22f69f0SBarry Smith 18936a4d4014SLisandro Dalcin PetscFunctionBegin; 18940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1895193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 18965a3a76d0SJed Brown if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 18975a3a76d0SJed Brown if (!ts->vec_sol || x == ts->vec_sol) { 18985a3a76d0SJed Brown Vec y; 18995a3a76d0SJed Brown ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 19005a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 19015a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 19025a3a76d0SJed Brown } 1903362cd11cSLisandro Dalcin ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr); 19045a3a76d0SJed Brown } else { 1905193ac0bcSJed Brown ierr = TSSetSolution(ts,x);CHKERRQ(ierr); 19065a3a76d0SJed Brown } 1907b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 19086a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 1909193ac0bcSJed Brown ts->steps = 0; 1910193ac0bcSJed Brown ts->linear_its = 0; 1911193ac0bcSJed Brown ts->nonlinear_its = 0; 1912c610991cSLisandro Dalcin ts->num_snes_failures = 0; 1913c610991cSLisandro Dalcin ts->reject = 0; 1914193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 1915193ac0bcSJed Brown 1916193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 1917193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 19185a3a76d0SJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 1919a5b82c69SSean Farley if (ftime) *ftime = ts->ptime; 1920193ac0bcSJed Brown } else { 19216a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 1922362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1923362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 1924362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 1925362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 1926362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 1927e1a7a14fSJed Brown while (!ts->reason) { 1928193ac0bcSJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 1929193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 1930193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 1931193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 1932193ac0bcSJed Brown } 1933362cd11cSLisandro Dalcin if (ts->exact_final_time && ts->ptime > ts->max_time) { 1934a43b19c4SJed Brown ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr); 19355a3a76d0SJed Brown if (ftime) *ftime = ts->max_time; 19360574a7fbSJed Brown } else { 19370574a7fbSJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 19380574a7fbSJed Brown if (ftime) *ftime = ts->ptime; 19390574a7fbSJed Brown } 1940193ac0bcSJed Brown } 19414d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 19424d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 19434d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 19444d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 19454d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1946193ac0bcSJed Brown } 19476a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19486a4d4014SLisandro Dalcin } 19496a4d4014SLisandro Dalcin 19506a4d4014SLisandro Dalcin #undef __FUNCT__ 19514a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1952228d79bcSJed Brown /*@ 1953228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 1954228d79bcSJed Brown 1955228d79bcSJed Brown Collective on TS 1956228d79bcSJed Brown 1957228d79bcSJed Brown Input Parameters: 1958228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 1959228d79bcSJed Brown . step - step number that has just completed 1960228d79bcSJed Brown . ptime - model time of the state 1961228d79bcSJed Brown - x - state at the current model time 1962228d79bcSJed Brown 1963228d79bcSJed Brown Notes: 1964228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 1965228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 1966228d79bcSJed Brown 1967228d79bcSJed Brown Level: advanced 1968228d79bcSJed Brown 1969228d79bcSJed Brown .keywords: TS, timestep 1970228d79bcSJed Brown @*/ 1971a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1972d763cef2SBarry Smith { 19736849ba73SBarry Smith PetscErrorCode ierr; 1974a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith PetscFunctionBegin; 1977d763cef2SBarry Smith for (i=0; i<n; i++) { 1978142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1979d763cef2SBarry Smith } 1980d763cef2SBarry Smith PetscFunctionReturn(0); 1981d763cef2SBarry Smith } 1982d763cef2SBarry Smith 1983d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1984d763cef2SBarry Smith 19854a2ae208SSatish Balay #undef __FUNCT__ 1986a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1987d763cef2SBarry Smith /*@C 1988a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1989d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith Collective on TS 1992d763cef2SBarry Smith 1993d763cef2SBarry Smith Input Parameters: 1994d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1995d763cef2SBarry Smith . label - the title to put in the title bar 19967c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1997d763cef2SBarry Smith - m, n - the screen width and height in pixels 1998d763cef2SBarry Smith 1999d763cef2SBarry Smith Output Parameter: 2000d763cef2SBarry Smith . draw - the drawing context 2001d763cef2SBarry Smith 2002d763cef2SBarry Smith Options Database Key: 2003a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith Notes: 2006a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 2007d763cef2SBarry Smith 2008d763cef2SBarry Smith Level: intermediate 2009d763cef2SBarry Smith 20107c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2011d763cef2SBarry Smith 2012a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 20137c922b88SBarry Smith 2014d763cef2SBarry Smith @*/ 20157087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2016d763cef2SBarry Smith { 2017b0a32e0cSBarry Smith PetscDraw win; 2018dfbe8321SBarry Smith PetscErrorCode ierr; 2019d763cef2SBarry Smith 2020d763cef2SBarry Smith PetscFunctionBegin; 2021b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2022b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 2023b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 2024b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 2025d763cef2SBarry Smith 202652e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 2027d763cef2SBarry Smith PetscFunctionReturn(0); 2028d763cef2SBarry Smith } 2029d763cef2SBarry Smith 20304a2ae208SSatish Balay #undef __FUNCT__ 2031a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 2032a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2033d763cef2SBarry Smith { 2034b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 203587828ca2SBarry Smith PetscReal x,y = ptime; 2036dfbe8321SBarry Smith PetscErrorCode ierr; 2037d763cef2SBarry Smith 2038d763cef2SBarry Smith PetscFunctionBegin; 20397c922b88SBarry Smith if (!monctx) { 20407c922b88SBarry Smith MPI_Comm comm; 2041b0a32e0cSBarry Smith PetscViewer viewer; 20427c922b88SBarry Smith 20437c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 2044b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 2045b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 20467c922b88SBarry Smith } 20477c922b88SBarry Smith 2048b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 204987828ca2SBarry Smith x = (PetscReal)n; 2050b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2051d763cef2SBarry Smith if (n < 20 || (n % 5)) { 2052b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2053d763cef2SBarry Smith } 2054d763cef2SBarry Smith PetscFunctionReturn(0); 2055d763cef2SBarry Smith } 2056d763cef2SBarry Smith 20574a2ae208SSatish Balay #undef __FUNCT__ 2058a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 2059d763cef2SBarry Smith /*@C 2060a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 2061a6570f20SBarry Smith with TSMonitorLGCreate(). 2062d763cef2SBarry Smith 2063b0a32e0cSBarry Smith Collective on PetscDrawLG 2064d763cef2SBarry Smith 2065d763cef2SBarry Smith Input Parameter: 2066d763cef2SBarry Smith . draw - the drawing context 2067d763cef2SBarry Smith 2068d763cef2SBarry Smith Level: intermediate 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2071d763cef2SBarry Smith 2072a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 2073d763cef2SBarry Smith @*/ 20746bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 2075d763cef2SBarry Smith { 2076b0a32e0cSBarry Smith PetscDraw draw; 2077dfbe8321SBarry Smith PetscErrorCode ierr; 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith PetscFunctionBegin; 20806bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 20816bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 2082b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 2083d763cef2SBarry Smith PetscFunctionReturn(0); 2084d763cef2SBarry Smith } 2085d763cef2SBarry Smith 20864a2ae208SSatish Balay #undef __FUNCT__ 20874a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2088d763cef2SBarry Smith /*@ 2089d763cef2SBarry Smith TSGetTime - Gets the current time. 2090d763cef2SBarry Smith 2091d763cef2SBarry Smith Not Collective 2092d763cef2SBarry Smith 2093d763cef2SBarry Smith Input Parameter: 2094d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith Output Parameter: 2097d763cef2SBarry Smith . t - the current time 2098d763cef2SBarry Smith 2099d763cef2SBarry Smith Level: beginner 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2102d763cef2SBarry Smith 2103d763cef2SBarry Smith .keywords: TS, get, time 2104d763cef2SBarry Smith @*/ 21057087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2106d763cef2SBarry Smith { 2107d763cef2SBarry Smith PetscFunctionBegin; 21080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21094482741eSBarry Smith PetscValidDoublePointer(t,2); 2110d763cef2SBarry Smith *t = ts->ptime; 2111d763cef2SBarry Smith PetscFunctionReturn(0); 2112d763cef2SBarry Smith } 2113d763cef2SBarry Smith 21144a2ae208SSatish Balay #undef __FUNCT__ 21156a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 21166a4d4014SLisandro Dalcin /*@ 21176a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 21186a4d4014SLisandro Dalcin 21193f9fe445SBarry Smith Logically Collective on TS 21206a4d4014SLisandro Dalcin 21216a4d4014SLisandro Dalcin Input Parameters: 21226a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 21236a4d4014SLisandro Dalcin - time - the time 21246a4d4014SLisandro Dalcin 21256a4d4014SLisandro Dalcin Level: intermediate 21266a4d4014SLisandro Dalcin 21276a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 21286a4d4014SLisandro Dalcin 21296a4d4014SLisandro Dalcin .keywords: TS, set, time 21306a4d4014SLisandro Dalcin @*/ 21317087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 21326a4d4014SLisandro Dalcin { 21336a4d4014SLisandro Dalcin PetscFunctionBegin; 21340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2135c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 21366a4d4014SLisandro Dalcin ts->ptime = t; 21376a4d4014SLisandro Dalcin PetscFunctionReturn(0); 21386a4d4014SLisandro Dalcin } 21396a4d4014SLisandro Dalcin 21406a4d4014SLisandro Dalcin #undef __FUNCT__ 21414a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2142d763cef2SBarry Smith /*@C 2143d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2144d763cef2SBarry Smith TS options in the database. 2145d763cef2SBarry Smith 21463f9fe445SBarry Smith Logically Collective on TS 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith Input Parameter: 2149d763cef2SBarry Smith + ts - The TS context 2150d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2151d763cef2SBarry Smith 2152d763cef2SBarry Smith Notes: 2153d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2154d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2155d763cef2SBarry Smith hyphen. 2156d763cef2SBarry Smith 2157d763cef2SBarry Smith Level: advanced 2158d763cef2SBarry Smith 2159d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2160d763cef2SBarry Smith 2161d763cef2SBarry Smith .seealso: TSSetFromOptions() 2162d763cef2SBarry Smith 2163d763cef2SBarry Smith @*/ 21647087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2165d763cef2SBarry Smith { 2166dfbe8321SBarry Smith PetscErrorCode ierr; 2167089b2837SJed Brown SNES snes; 2168d763cef2SBarry Smith 2169d763cef2SBarry Smith PetscFunctionBegin; 21700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2171d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2172089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2173089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2174d763cef2SBarry Smith PetscFunctionReturn(0); 2175d763cef2SBarry Smith } 2176d763cef2SBarry Smith 2177d763cef2SBarry Smith 21784a2ae208SSatish Balay #undef __FUNCT__ 21794a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2180d763cef2SBarry Smith /*@C 2181d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2182d763cef2SBarry Smith TS options in the database. 2183d763cef2SBarry Smith 21843f9fe445SBarry Smith Logically Collective on TS 2185d763cef2SBarry Smith 2186d763cef2SBarry Smith Input Parameter: 2187d763cef2SBarry Smith + ts - The TS context 2188d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2189d763cef2SBarry Smith 2190d763cef2SBarry Smith Notes: 2191d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2192d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2193d763cef2SBarry Smith hyphen. 2194d763cef2SBarry Smith 2195d763cef2SBarry Smith Level: advanced 2196d763cef2SBarry Smith 2197d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2198d763cef2SBarry Smith 2199d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2200d763cef2SBarry Smith 2201d763cef2SBarry Smith @*/ 22027087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2203d763cef2SBarry Smith { 2204dfbe8321SBarry Smith PetscErrorCode ierr; 2205089b2837SJed Brown SNES snes; 2206d763cef2SBarry Smith 2207d763cef2SBarry Smith PetscFunctionBegin; 22080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2209d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2210089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2211089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2212d763cef2SBarry Smith PetscFunctionReturn(0); 2213d763cef2SBarry Smith } 2214d763cef2SBarry Smith 22154a2ae208SSatish Balay #undef __FUNCT__ 22164a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2217d763cef2SBarry Smith /*@C 2218d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2219d763cef2SBarry Smith TS options in the database. 2220d763cef2SBarry Smith 2221d763cef2SBarry Smith Not Collective 2222d763cef2SBarry Smith 2223d763cef2SBarry Smith Input Parameter: 2224d763cef2SBarry Smith . ts - The TS context 2225d763cef2SBarry Smith 2226d763cef2SBarry Smith Output Parameter: 2227d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2228d763cef2SBarry Smith 2229d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2230d763cef2SBarry Smith sufficient length to hold the prefix. 2231d763cef2SBarry Smith 2232d763cef2SBarry Smith Level: intermediate 2233d763cef2SBarry Smith 2234d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2235d763cef2SBarry Smith 2236d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2237d763cef2SBarry Smith @*/ 22387087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2239d763cef2SBarry Smith { 2240dfbe8321SBarry Smith PetscErrorCode ierr; 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith PetscFunctionBegin; 22430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22444482741eSBarry Smith PetscValidPointer(prefix,2); 2245d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2246d763cef2SBarry Smith PetscFunctionReturn(0); 2247d763cef2SBarry Smith } 2248d763cef2SBarry Smith 22494a2ae208SSatish Balay #undef __FUNCT__ 22504a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2251d763cef2SBarry Smith /*@C 2252d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2253d763cef2SBarry Smith 2254d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2255d763cef2SBarry Smith 2256d763cef2SBarry Smith Input Parameter: 2257d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2258d763cef2SBarry Smith 2259d763cef2SBarry Smith Output Parameters: 2260d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2261d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2262089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2263d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2264d763cef2SBarry Smith 2265d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2266d763cef2SBarry Smith 2267d763cef2SBarry Smith Level: intermediate 2268d763cef2SBarry Smith 226926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2270d763cef2SBarry Smith 2271d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2272d763cef2SBarry Smith @*/ 2273089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2274d763cef2SBarry Smith { 2275089b2837SJed Brown PetscErrorCode ierr; 2276089b2837SJed Brown SNES snes; 2277089b2837SJed Brown 2278d763cef2SBarry Smith PetscFunctionBegin; 2279089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2280089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 22813b5f76d0SSean Farley if (func) *func = ts->userops->rhsjacobian; 228226d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2283d763cef2SBarry Smith PetscFunctionReturn(0); 2284d763cef2SBarry Smith } 2285d763cef2SBarry Smith 22861713a123SBarry Smith #undef __FUNCT__ 22872eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 22882eca1d9cSJed Brown /*@C 22892eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 22902eca1d9cSJed Brown 22912eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 22922eca1d9cSJed Brown 22932eca1d9cSJed Brown Input Parameter: 22942eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 22952eca1d9cSJed Brown 22962eca1d9cSJed Brown Output Parameters: 22972eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 22982eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 22992eca1d9cSJed Brown . f - The function to compute the matrices 23002eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 23012eca1d9cSJed Brown 23022eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 23032eca1d9cSJed Brown 23042eca1d9cSJed Brown Level: advanced 23052eca1d9cSJed Brown 23062eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 23072eca1d9cSJed Brown 23082eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 23092eca1d9cSJed Brown @*/ 23107087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 23112eca1d9cSJed Brown { 2312089b2837SJed Brown PetscErrorCode ierr; 2313089b2837SJed Brown SNES snes; 2314089b2837SJed Brown 23152eca1d9cSJed Brown PetscFunctionBegin; 2316089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2317089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 23183b5f76d0SSean Farley if (f) *f = ts->userops->ijacobian; 23192eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 23202eca1d9cSJed Brown PetscFunctionReturn(0); 23212eca1d9cSJed Brown } 23222eca1d9cSJed Brown 23236083293cSBarry Smith typedef struct { 23246083293cSBarry Smith PetscViewer viewer; 23256083293cSBarry Smith Vec initialsolution; 23266083293cSBarry Smith PetscBool showinitial; 23276083293cSBarry Smith } TSMonitorSolutionCtx; 23286083293cSBarry Smith 23292eca1d9cSJed Brown #undef __FUNCT__ 2330a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 23311713a123SBarry Smith /*@C 2332a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 23331713a123SBarry Smith VecView() for the solution at each timestep 23341713a123SBarry Smith 23351713a123SBarry Smith Collective on TS 23361713a123SBarry Smith 23371713a123SBarry Smith Input Parameters: 23381713a123SBarry Smith + ts - the TS context 23391713a123SBarry Smith . step - current time-step 2340142b95e3SSatish Balay . ptime - current time 23411713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 23421713a123SBarry Smith 23431713a123SBarry Smith Level: intermediate 23441713a123SBarry Smith 23451713a123SBarry Smith .keywords: TS, vector, monitor, view 23461713a123SBarry Smith 2347a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 23481713a123SBarry Smith @*/ 23497087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 23501713a123SBarry Smith { 2351dfbe8321SBarry Smith PetscErrorCode ierr; 23526083293cSBarry Smith TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy; 23531713a123SBarry Smith 23541713a123SBarry Smith PetscFunctionBegin; 23556083293cSBarry Smith if (!step && ictx->showinitial) { 23566083293cSBarry Smith if (!ictx->initialsolution) { 23576083293cSBarry Smith ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr); 23581713a123SBarry Smith } 23596083293cSBarry Smith ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr); 23606083293cSBarry Smith } 23616083293cSBarry Smith if (ictx->showinitial) { 23626083293cSBarry Smith PetscReal pause; 23636083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 23646083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 23656083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 23666083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 23676083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 23686083293cSBarry Smith } 23696083293cSBarry Smith ierr = VecView(x,ictx->viewer);CHKERRQ(ierr); 23706083293cSBarry Smith if (ictx->showinitial) { 23716083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 23726083293cSBarry Smith } 23731713a123SBarry Smith PetscFunctionReturn(0); 23741713a123SBarry Smith } 23751713a123SBarry Smith 23761713a123SBarry Smith 23776c699258SBarry Smith #undef __FUNCT__ 23786083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy" 23796083293cSBarry Smith /*@C 23806083293cSBarry Smith TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution 23816083293cSBarry Smith 23826083293cSBarry Smith Collective on TS 23836083293cSBarry Smith 23846083293cSBarry Smith Input Parameters: 23856083293cSBarry Smith . ctx - the monitor context 23866083293cSBarry Smith 23876083293cSBarry Smith Level: intermediate 23886083293cSBarry Smith 23896083293cSBarry Smith .keywords: TS, vector, monitor, view 23906083293cSBarry Smith 23916083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 23926083293cSBarry Smith @*/ 23936083293cSBarry Smith PetscErrorCode TSMonitorSolutionDestroy(void **ctx) 23946083293cSBarry Smith { 23956083293cSBarry Smith PetscErrorCode ierr; 23966083293cSBarry Smith TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx; 23976083293cSBarry Smith 23986083293cSBarry Smith PetscFunctionBegin; 23996083293cSBarry Smith ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr); 24006083293cSBarry Smith ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr); 24016083293cSBarry Smith ierr = PetscFree(ictx);CHKERRQ(ierr); 24026083293cSBarry Smith PetscFunctionReturn(0); 24036083293cSBarry Smith } 24046083293cSBarry Smith 24056083293cSBarry Smith #undef __FUNCT__ 24066083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate" 24076083293cSBarry Smith /*@C 24086083293cSBarry Smith TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution 24096083293cSBarry Smith 24106083293cSBarry Smith Collective on TS 24116083293cSBarry Smith 24126083293cSBarry Smith Input Parameter: 24136083293cSBarry Smith . ts - time-step context 24146083293cSBarry Smith 24156083293cSBarry Smith Output Patameter: 24166083293cSBarry Smith . ctx - the monitor context 24176083293cSBarry Smith 24186083293cSBarry Smith Level: intermediate 24196083293cSBarry Smith 24206083293cSBarry Smith .keywords: TS, vector, monitor, view 24216083293cSBarry Smith 24226083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 24236083293cSBarry Smith @*/ 24246083293cSBarry Smith PetscErrorCode TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx) 24256083293cSBarry Smith { 24266083293cSBarry Smith PetscErrorCode ierr; 24276083293cSBarry Smith TSMonitorSolutionCtx *ictx; 24286083293cSBarry Smith 24296083293cSBarry Smith PetscFunctionBegin; 24306083293cSBarry Smith ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr); 24316083293cSBarry Smith *ctx = (void*)ictx; 24326083293cSBarry Smith if (!viewer) { 24336083293cSBarry Smith viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 24346083293cSBarry Smith } 24356083293cSBarry Smith ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 24366083293cSBarry Smith ictx->viewer = viewer; 24376083293cSBarry Smith ictx->showinitial = PETSC_FALSE; 24386083293cSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr); 24396083293cSBarry Smith PetscFunctionReturn(0); 24406083293cSBarry Smith } 24416083293cSBarry Smith 24426083293cSBarry Smith #undef __FUNCT__ 24436c699258SBarry Smith #define __FUNCT__ "TSSetDM" 24446c699258SBarry Smith /*@ 24456c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 24466c699258SBarry Smith 24473f9fe445SBarry Smith Logically Collective on TS and DM 24486c699258SBarry Smith 24496c699258SBarry Smith Input Parameters: 24506c699258SBarry Smith + ts - the preconditioner context 24516c699258SBarry Smith - dm - the dm 24526c699258SBarry Smith 24536c699258SBarry Smith Level: intermediate 24546c699258SBarry Smith 24556c699258SBarry Smith 24566c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 24576c699258SBarry Smith @*/ 24587087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 24596c699258SBarry Smith { 24606c699258SBarry Smith PetscErrorCode ierr; 2461089b2837SJed Brown SNES snes; 24626c699258SBarry Smith 24636c699258SBarry Smith PetscFunctionBegin; 24640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 246570663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 24666bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 24676c699258SBarry Smith ts->dm = dm; 2468089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2469089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 24706c699258SBarry Smith PetscFunctionReturn(0); 24716c699258SBarry Smith } 24726c699258SBarry Smith 24736c699258SBarry Smith #undef __FUNCT__ 24746c699258SBarry Smith #define __FUNCT__ "TSGetDM" 24756c699258SBarry Smith /*@ 24766c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 24776c699258SBarry Smith 24783f9fe445SBarry Smith Not Collective 24796c699258SBarry Smith 24806c699258SBarry Smith Input Parameter: 24816c699258SBarry Smith . ts - the preconditioner context 24826c699258SBarry Smith 24836c699258SBarry Smith Output Parameter: 24846c699258SBarry Smith . dm - the dm 24856c699258SBarry Smith 24866c699258SBarry Smith Level: intermediate 24876c699258SBarry Smith 24886c699258SBarry Smith 24896c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 24906c699258SBarry Smith @*/ 24917087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 24926c699258SBarry Smith { 24936c699258SBarry Smith PetscFunctionBegin; 24940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24956c699258SBarry Smith *dm = ts->dm; 24966c699258SBarry Smith PetscFunctionReturn(0); 24976c699258SBarry Smith } 24981713a123SBarry Smith 24990f5c6efeSJed Brown #undef __FUNCT__ 25000f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 25010f5c6efeSJed Brown /*@ 25020f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 25030f5c6efeSJed Brown 25043f9fe445SBarry Smith Logically Collective on SNES 25050f5c6efeSJed Brown 25060f5c6efeSJed Brown Input Parameter: 2507d42a1c89SJed Brown + snes - nonlinear solver 25080f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2509d42a1c89SJed Brown - ctx - user context, must be a TS 25100f5c6efeSJed Brown 25110f5c6efeSJed Brown Output Parameter: 25120f5c6efeSJed Brown . F - the nonlinear residual 25130f5c6efeSJed Brown 25140f5c6efeSJed Brown Notes: 25150f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 25160f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 25170f5c6efeSJed Brown 25180f5c6efeSJed Brown Level: advanced 25190f5c6efeSJed Brown 25200f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 25210f5c6efeSJed Brown @*/ 25227087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 25230f5c6efeSJed Brown { 25240f5c6efeSJed Brown TS ts = (TS)ctx; 25250f5c6efeSJed Brown PetscErrorCode ierr; 25260f5c6efeSJed Brown 25270f5c6efeSJed Brown PetscFunctionBegin; 25280f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25290f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 25300f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 25310f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 25320f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 25330f5c6efeSJed Brown PetscFunctionReturn(0); 25340f5c6efeSJed Brown } 25350f5c6efeSJed Brown 25360f5c6efeSJed Brown #undef __FUNCT__ 25370f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 25380f5c6efeSJed Brown /*@ 25390f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 25400f5c6efeSJed Brown 25410f5c6efeSJed Brown Collective on SNES 25420f5c6efeSJed Brown 25430f5c6efeSJed Brown Input Parameter: 25440f5c6efeSJed Brown + snes - nonlinear solver 25450f5c6efeSJed Brown . X - the current state at which to evaluate the residual 25460f5c6efeSJed Brown - ctx - user context, must be a TS 25470f5c6efeSJed Brown 25480f5c6efeSJed Brown Output Parameter: 25490f5c6efeSJed Brown + A - the Jacobian 25500f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 25510f5c6efeSJed Brown - flag - indicates any structure change in the matrix 25520f5c6efeSJed Brown 25530f5c6efeSJed Brown Notes: 25540f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 25550f5c6efeSJed Brown 25560f5c6efeSJed Brown Level: developer 25570f5c6efeSJed Brown 25580f5c6efeSJed Brown .seealso: SNESSetJacobian() 25590f5c6efeSJed Brown @*/ 25607087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 25610f5c6efeSJed Brown { 25620f5c6efeSJed Brown TS ts = (TS)ctx; 25630f5c6efeSJed Brown PetscErrorCode ierr; 25640f5c6efeSJed Brown 25650f5c6efeSJed Brown PetscFunctionBegin; 25660f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25670f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 25680f5c6efeSJed Brown PetscValidPointer(A,3); 25690f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 25700f5c6efeSJed Brown PetscValidPointer(B,4); 25710f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 25720f5c6efeSJed Brown PetscValidPointer(flag,5); 25730f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 25740f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 25750f5c6efeSJed Brown PetscFunctionReturn(0); 25760f5c6efeSJed Brown } 2577325fc9f4SBarry Smith 25780e4ef248SJed Brown #undef __FUNCT__ 25790e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 25800e4ef248SJed Brown /*@C 25810e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 25820e4ef248SJed Brown 25830e4ef248SJed Brown Collective on TS 25840e4ef248SJed Brown 25850e4ef248SJed Brown Input Arguments: 25860e4ef248SJed Brown + ts - time stepping context 25870e4ef248SJed Brown . t - time at which to evaluate 25880e4ef248SJed Brown . X - state at which to evaluate 25890e4ef248SJed Brown - ctx - context 25900e4ef248SJed Brown 25910e4ef248SJed Brown Output Arguments: 25920e4ef248SJed Brown . F - right hand side 25930e4ef248SJed Brown 25940e4ef248SJed Brown Level: intermediate 25950e4ef248SJed Brown 25960e4ef248SJed Brown Notes: 25970e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 25980e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 25990e4ef248SJed Brown 26000e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 26010e4ef248SJed Brown @*/ 26020e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 26030e4ef248SJed Brown { 26040e4ef248SJed Brown PetscErrorCode ierr; 26050e4ef248SJed Brown Mat Arhs,Brhs; 26060e4ef248SJed Brown MatStructure flg2; 26070e4ef248SJed Brown 26080e4ef248SJed Brown PetscFunctionBegin; 26090e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 26100e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 26110e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 26120e4ef248SJed Brown PetscFunctionReturn(0); 26130e4ef248SJed Brown } 26140e4ef248SJed Brown 26150e4ef248SJed Brown #undef __FUNCT__ 26160e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 26170e4ef248SJed Brown /*@C 26180e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 26190e4ef248SJed Brown 26200e4ef248SJed Brown Collective on TS 26210e4ef248SJed Brown 26220e4ef248SJed Brown Input Arguments: 26230e4ef248SJed Brown + ts - time stepping context 26240e4ef248SJed Brown . t - time at which to evaluate 26250e4ef248SJed Brown . X - state at which to evaluate 26260e4ef248SJed Brown - ctx - context 26270e4ef248SJed Brown 26280e4ef248SJed Brown Output Arguments: 26290e4ef248SJed Brown + A - pointer to operator 26300e4ef248SJed Brown . B - pointer to preconditioning matrix 26310e4ef248SJed Brown - flg - matrix structure flag 26320e4ef248SJed Brown 26330e4ef248SJed Brown Level: intermediate 26340e4ef248SJed Brown 26350e4ef248SJed Brown Notes: 26360e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 26370e4ef248SJed Brown 26380e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 26390e4ef248SJed Brown @*/ 26400e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 26410e4ef248SJed Brown { 26420e4ef248SJed Brown 26430e4ef248SJed Brown PetscFunctionBegin; 26440e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 26450e4ef248SJed Brown PetscFunctionReturn(0); 26460e4ef248SJed Brown } 26470e4ef248SJed Brown 26480026cea9SSean Farley #undef __FUNCT__ 26490026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 26500026cea9SSean Farley /*@C 26510026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 26520026cea9SSean Farley 26530026cea9SSean Farley Collective on TS 26540026cea9SSean Farley 26550026cea9SSean Farley Input Arguments: 26560026cea9SSean Farley + ts - time stepping context 26570026cea9SSean Farley . t - time at which to evaluate 26580026cea9SSean Farley . X - state at which to evaluate 26590026cea9SSean Farley . Xdot - time derivative of state vector 26600026cea9SSean Farley - ctx - context 26610026cea9SSean Farley 26620026cea9SSean Farley Output Arguments: 26630026cea9SSean Farley . F - left hand side 26640026cea9SSean Farley 26650026cea9SSean Farley Level: intermediate 26660026cea9SSean Farley 26670026cea9SSean Farley Notes: 26680026cea9SSean 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 26690026cea9SSean Farley user is required to write their own TSComputeIFunction. 26700026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 26710026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 26720026cea9SSean Farley 26730026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 26740026cea9SSean Farley @*/ 26750026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 26760026cea9SSean Farley { 26770026cea9SSean Farley PetscErrorCode ierr; 26780026cea9SSean Farley Mat A,B; 26790026cea9SSean Farley MatStructure flg2; 26800026cea9SSean Farley 26810026cea9SSean Farley PetscFunctionBegin; 26820026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 26830026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 26840026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 26850026cea9SSean Farley PetscFunctionReturn(0); 26860026cea9SSean Farley } 26870026cea9SSean Farley 26880026cea9SSean Farley #undef __FUNCT__ 26890026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 26900026cea9SSean Farley /*@C 269124e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 26920026cea9SSean Farley 26930026cea9SSean Farley Collective on TS 26940026cea9SSean Farley 26950026cea9SSean Farley Input Arguments: 26960026cea9SSean Farley + ts - time stepping context 26970026cea9SSean Farley . t - time at which to evaluate 26980026cea9SSean Farley . X - state at which to evaluate 26990026cea9SSean Farley . Xdot - time derivative of state vector 27000026cea9SSean Farley . shift - shift to apply 27010026cea9SSean Farley - ctx - context 27020026cea9SSean Farley 27030026cea9SSean Farley Output Arguments: 27040026cea9SSean Farley + A - pointer to operator 27050026cea9SSean Farley . B - pointer to preconditioning matrix 27060026cea9SSean Farley - flg - matrix structure flag 27070026cea9SSean Farley 27080026cea9SSean Farley Level: intermediate 27090026cea9SSean Farley 27100026cea9SSean Farley Notes: 27110026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 27120026cea9SSean Farley 27130026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 27140026cea9SSean Farley @*/ 27150026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 27160026cea9SSean Farley { 27170026cea9SSean Farley 27180026cea9SSean Farley PetscFunctionBegin; 27190026cea9SSean Farley *flg = SAME_PRECONDITIONER; 27200026cea9SSean Farley PetscFunctionReturn(0); 27210026cea9SSean Farley } 27220026cea9SSean Farley 27234af1b03aSJed Brown 27244af1b03aSJed Brown #undef __FUNCT__ 27254af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 27264af1b03aSJed Brown /*@ 27274af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 27284af1b03aSJed Brown 27294af1b03aSJed Brown Not Collective 27304af1b03aSJed Brown 27314af1b03aSJed Brown Input Parameter: 27324af1b03aSJed Brown . ts - the TS context 27334af1b03aSJed Brown 27344af1b03aSJed Brown Output Parameter: 27354af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 27364af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 27374af1b03aSJed Brown 27384af1b03aSJed Brown Level: intermediate 27394af1b03aSJed Brown 2740cd652676SJed Brown Notes: 2741cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 27424af1b03aSJed Brown 27434af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 27444af1b03aSJed Brown 27454af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 27464af1b03aSJed Brown @*/ 27474af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 27484af1b03aSJed Brown { 27494af1b03aSJed Brown PetscFunctionBegin; 27504af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27514af1b03aSJed Brown PetscValidPointer(reason,2); 27524af1b03aSJed Brown *reason = ts->reason; 27534af1b03aSJed Brown PetscFunctionReturn(0); 27544af1b03aSJed Brown } 27554af1b03aSJed Brown 2756fb1732b5SBarry Smith #undef __FUNCT__ 27579f67acb7SJed Brown #define __FUNCT__ "TSGetNonlinearSolveIterations" 27589f67acb7SJed Brown /*@ 275901c7e25bSJed Brown TSGetNonlinearSolveIterations - Gets the total number of linear iterations 27609f67acb7SJed Brown used by the time integrator. 27619f67acb7SJed Brown 27629f67acb7SJed Brown Not Collective 27639f67acb7SJed Brown 27649f67acb7SJed Brown Input Parameter: 27659f67acb7SJed Brown . ts - TS context 27669f67acb7SJed Brown 27679f67acb7SJed Brown Output Parameter: 27689f67acb7SJed Brown . nits - number of nonlinear iterations 27699f67acb7SJed Brown 27709f67acb7SJed Brown Notes: 27719f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 27729f67acb7SJed Brown 27739f67acb7SJed Brown Level: intermediate 27749f67acb7SJed Brown 27759f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 27769f67acb7SJed Brown 27779f67acb7SJed Brown .seealso: TSGetLinearSolveIterations() 27789f67acb7SJed Brown @*/ 27799f67acb7SJed Brown PetscErrorCode TSGetNonlinearSolveIterations(TS ts,PetscInt *nits) 27809f67acb7SJed Brown { 27819f67acb7SJed Brown PetscFunctionBegin; 27829f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27839f67acb7SJed Brown PetscValidIntPointer(nits,2); 27849f67acb7SJed Brown *nits = ts->nonlinear_its; 27859f67acb7SJed Brown PetscFunctionReturn(0); 27869f67acb7SJed Brown } 27879f67acb7SJed Brown 27889f67acb7SJed Brown #undef __FUNCT__ 27899f67acb7SJed Brown #define __FUNCT__ "TSGetLinearSolveIterations" 27909f67acb7SJed Brown /*@ 27919f67acb7SJed Brown TSGetLinearSolveIterations - Gets the total number of linear iterations 27929f67acb7SJed Brown used by the time integrator. 27939f67acb7SJed Brown 27949f67acb7SJed Brown Not Collective 27959f67acb7SJed Brown 27969f67acb7SJed Brown Input Parameter: 27979f67acb7SJed Brown . ts - TS context 27989f67acb7SJed Brown 27999f67acb7SJed Brown Output Parameter: 28009f67acb7SJed Brown . lits - number of linear iterations 28019f67acb7SJed Brown 28029f67acb7SJed Brown Notes: 28039f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 28049f67acb7SJed Brown 28059f67acb7SJed Brown Level: intermediate 28069f67acb7SJed Brown 28079f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 28089f67acb7SJed Brown 28099f67acb7SJed Brown .seealso: TSGetNonlinearSolveIterations(), SNESGetLinearSolveIterations() 28109f67acb7SJed Brown @*/ 28119f67acb7SJed Brown PetscErrorCode TSGetLinearSolveIterations(TS ts,PetscInt *lits) 28129f67acb7SJed Brown { 28139f67acb7SJed Brown PetscFunctionBegin; 28149f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28159f67acb7SJed Brown PetscValidIntPointer(lits,2); 28169f67acb7SJed Brown *lits = ts->linear_its; 28179f67acb7SJed Brown PetscFunctionReturn(0); 28189f67acb7SJed Brown } 28199f67acb7SJed Brown 28209f67acb7SJed Brown #undef __FUNCT__ 2821fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 2822fb1732b5SBarry Smith /*@C 2823fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 2824fb1732b5SBarry Smith 2825fb1732b5SBarry Smith Collective on TS 2826fb1732b5SBarry Smith 2827fb1732b5SBarry Smith Input Parameters: 2828fb1732b5SBarry Smith + ts - the TS context 2829fb1732b5SBarry Smith . step - current time-step 2830fb1732b5SBarry Smith . ptime - current time 2831ed81e22dSJed Brown . x - current state 2832fb1732b5SBarry Smith - viewer - binary viewer 2833fb1732b5SBarry Smith 2834fb1732b5SBarry Smith Level: intermediate 2835fb1732b5SBarry Smith 2836fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 2837fb1732b5SBarry Smith 2838fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2839fb1732b5SBarry Smith @*/ 2840ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer) 2841fb1732b5SBarry Smith { 2842fb1732b5SBarry Smith PetscErrorCode ierr; 2843ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 2844fb1732b5SBarry Smith 2845fb1732b5SBarry Smith PetscFunctionBegin; 2846ed81e22dSJed Brown ierr = VecView(x,v);CHKERRQ(ierr); 2847ed81e22dSJed Brown PetscFunctionReturn(0); 2848ed81e22dSJed Brown } 2849ed81e22dSJed Brown 2850ed81e22dSJed Brown #undef __FUNCT__ 2851ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 2852ed81e22dSJed Brown /*@C 2853ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 2854ed81e22dSJed Brown 2855ed81e22dSJed Brown Collective on TS 2856ed81e22dSJed Brown 2857ed81e22dSJed Brown Input Parameters: 2858ed81e22dSJed Brown + ts - the TS context 2859ed81e22dSJed Brown . step - current time-step 2860ed81e22dSJed Brown . ptime - current time 2861ed81e22dSJed Brown . x - current state 2862ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 2863ed81e22dSJed Brown 2864ed81e22dSJed Brown Level: intermediate 2865ed81e22dSJed Brown 2866ed81e22dSJed Brown Notes: 2867ed81e22dSJed Brown The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step. 2868ed81e22dSJed Brown These are named according to the file name template. 2869ed81e22dSJed Brown 2870ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 2871ed81e22dSJed Brown 2872ed81e22dSJed Brown .keywords: TS, vector, monitor, view 2873ed81e22dSJed Brown 2874ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2875ed81e22dSJed Brown @*/ 2876ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate) 2877ed81e22dSJed Brown { 2878ed81e22dSJed Brown PetscErrorCode ierr; 2879ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 2880ed81e22dSJed Brown PetscViewer viewer; 2881ed81e22dSJed Brown 2882ed81e22dSJed Brown PetscFunctionBegin; 2883ed81e22dSJed Brown ierr = PetscSNPrintf(filename,sizeof filename,(const char*)filenametemplate,step);CHKERRQ(ierr); 2884ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 2885fb1732b5SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 2886ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2887ed81e22dSJed Brown PetscFunctionReturn(0); 2888ed81e22dSJed Brown } 2889ed81e22dSJed Brown 2890ed81e22dSJed Brown #undef __FUNCT__ 2891ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 2892ed81e22dSJed Brown /*@C 2893ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 2894ed81e22dSJed Brown 2895ed81e22dSJed Brown Collective on TS 2896ed81e22dSJed Brown 2897ed81e22dSJed Brown Input Parameters: 2898ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 2899ed81e22dSJed Brown 2900ed81e22dSJed Brown Level: intermediate 2901ed81e22dSJed Brown 2902ed81e22dSJed Brown Note: 2903ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 2904ed81e22dSJed Brown 2905ed81e22dSJed Brown .keywords: TS, vector, monitor, view 2906ed81e22dSJed Brown 2907ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 2908ed81e22dSJed Brown @*/ 2909ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 2910ed81e22dSJed Brown { 2911ed81e22dSJed Brown PetscErrorCode ierr; 2912ed81e22dSJed Brown 2913ed81e22dSJed Brown PetscFunctionBegin; 2914ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 2915fb1732b5SBarry Smith PetscFunctionReturn(0); 2916fb1732b5SBarry Smith } 2917fb1732b5SBarry Smith 291884df9cb4SJed Brown #undef __FUNCT__ 291984df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt" 292084df9cb4SJed Brown /*@ 292184df9cb4SJed Brown TSGetAdapt - Get the adaptive controller context for the current method 292284df9cb4SJed Brown 2923ed81e22dSJed Brown Collective on TS if controller has not been created yet 292484df9cb4SJed Brown 292584df9cb4SJed Brown Input Arguments: 2926ed81e22dSJed Brown . ts - time stepping context 292784df9cb4SJed Brown 292884df9cb4SJed Brown Output Arguments: 2929ed81e22dSJed Brown . adapt - adaptive controller 293084df9cb4SJed Brown 293184df9cb4SJed Brown Level: intermediate 293284df9cb4SJed Brown 2933ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 293484df9cb4SJed Brown @*/ 293584df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 293684df9cb4SJed Brown { 293784df9cb4SJed Brown PetscErrorCode ierr; 293884df9cb4SJed Brown 293984df9cb4SJed Brown PetscFunctionBegin; 294084df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 294184df9cb4SJed Brown PetscValidPointer(adapt,2); 294284df9cb4SJed Brown if (!ts->adapt) { 294384df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 29441c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 29451c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 294684df9cb4SJed Brown } 294784df9cb4SJed Brown *adapt = ts->adapt; 294884df9cb4SJed Brown PetscFunctionReturn(0); 294984df9cb4SJed Brown } 2950d6ebe24aSShri Abhyankar 2951d6ebe24aSShri Abhyankar #undef __FUNCT__ 29521c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 29531c3436cfSJed Brown /*@ 29541c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 29551c3436cfSJed Brown 29561c3436cfSJed Brown Logically Collective 29571c3436cfSJed Brown 29581c3436cfSJed Brown Input Arguments: 29591c3436cfSJed Brown + ts - time integration context 29601c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 29611c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 29621c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 29631c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 29641c3436cfSJed Brown 29651c3436cfSJed Brown Level: beginner 29661c3436cfSJed Brown 29671c3436cfSJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS() 29681c3436cfSJed Brown @*/ 29691c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 29701c3436cfSJed Brown { 29711c3436cfSJed Brown PetscErrorCode ierr; 29721c3436cfSJed Brown 29731c3436cfSJed Brown PetscFunctionBegin; 29741c3436cfSJed Brown if (atol != PETSC_DECIDE) ts->atol = atol; 29751c3436cfSJed Brown if (vatol) { 29761c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 29771c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 29781c3436cfSJed Brown ts->vatol = vatol; 29791c3436cfSJed Brown } 29801c3436cfSJed Brown if (rtol != PETSC_DECIDE) ts->rtol = rtol; 29811c3436cfSJed Brown if (vrtol) { 29821c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 29831c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 29841c3436cfSJed Brown ts->vrtol = vrtol; 29851c3436cfSJed Brown } 29861c3436cfSJed Brown PetscFunctionReturn(0); 29871c3436cfSJed Brown } 29881c3436cfSJed Brown 29891c3436cfSJed Brown #undef __FUNCT__ 29901c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 29911c3436cfSJed Brown /*@ 29921c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 29931c3436cfSJed Brown 29941c3436cfSJed Brown Collective on TS 29951c3436cfSJed Brown 29961c3436cfSJed Brown Input Arguments: 29971c3436cfSJed Brown + ts - time stepping context 29981c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 29991c3436cfSJed Brown 30001c3436cfSJed Brown Output Arguments: 30011c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 30021c3436cfSJed Brown 30031c3436cfSJed Brown Level: developer 30041c3436cfSJed Brown 30051c3436cfSJed Brown .seealso: TSSetTolerances() 30061c3436cfSJed Brown @*/ 30071c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 30081c3436cfSJed Brown { 30091c3436cfSJed Brown PetscErrorCode ierr; 30101c3436cfSJed Brown PetscInt i,n,N; 30111c3436cfSJed Brown const PetscScalar *x,*y; 30121c3436cfSJed Brown Vec X; 30131c3436cfSJed Brown PetscReal sum,gsum; 30141c3436cfSJed Brown 30151c3436cfSJed Brown PetscFunctionBegin; 30161c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30171c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 30181c3436cfSJed Brown PetscValidPointer(norm,3); 30191c3436cfSJed Brown X = ts->vec_sol; 30201c3436cfSJed Brown PetscCheckSameTypeAndComm(X,1,Y,2); 30211c3436cfSJed Brown if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 30221c3436cfSJed Brown 30231c3436cfSJed Brown /* This is simple to implement, just not done yet */ 30241c3436cfSJed Brown if (ts->vatol || ts->vrtol) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"No support for vector scaling yet"); 30251c3436cfSJed Brown 30261c3436cfSJed Brown ierr = VecGetSize(X,&N);CHKERRQ(ierr); 30271c3436cfSJed Brown ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); 30281c3436cfSJed Brown ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); 30291c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 30301c3436cfSJed Brown sum = 0.; 30311c3436cfSJed Brown for (i=0; i<n; i++) { 30321c3436cfSJed Brown PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 30331c3436cfSJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 30341c3436cfSJed Brown } 30351c3436cfSJed Brown ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); 30361c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 30371c3436cfSJed Brown 30381c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 30391c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 30401c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 30411c3436cfSJed Brown PetscFunctionReturn(0); 30421c3436cfSJed Brown } 30431c3436cfSJed Brown 30441c3436cfSJed Brown #undef __FUNCT__ 30458d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 30468d59e960SJed Brown /*@ 30478d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 30488d59e960SJed Brown 30498d59e960SJed Brown Logically Collective on TS 30508d59e960SJed Brown 30518d59e960SJed Brown Input Arguments: 30528d59e960SJed Brown + ts - time stepping context 30538d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 30548d59e960SJed Brown 30558d59e960SJed Brown Note: 30568d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 30578d59e960SJed Brown 30588d59e960SJed Brown Level: intermediate 30598d59e960SJed Brown 30608d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 30618d59e960SJed Brown @*/ 30628d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 30638d59e960SJed Brown { 30648d59e960SJed Brown 30658d59e960SJed Brown PetscFunctionBegin; 30668d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30678d59e960SJed Brown ts->cfltime_local = cfltime; 30688d59e960SJed Brown ts->cfltime = -1.; 30698d59e960SJed Brown PetscFunctionReturn(0); 30708d59e960SJed Brown } 30718d59e960SJed Brown 30728d59e960SJed Brown #undef __FUNCT__ 30738d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 30748d59e960SJed Brown /*@ 30758d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 30768d59e960SJed Brown 30778d59e960SJed Brown Collective on TS 30788d59e960SJed Brown 30798d59e960SJed Brown Input Arguments: 30808d59e960SJed Brown . ts - time stepping context 30818d59e960SJed Brown 30828d59e960SJed Brown Output Arguments: 30838d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 30848d59e960SJed Brown 30858d59e960SJed Brown Level: advanced 30868d59e960SJed Brown 30878d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 30888d59e960SJed Brown @*/ 30898d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 30908d59e960SJed Brown { 30918d59e960SJed Brown PetscErrorCode ierr; 30928d59e960SJed Brown 30938d59e960SJed Brown PetscFunctionBegin; 30948d59e960SJed Brown if (ts->cfltime < 0) { 30958d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 30968d59e960SJed Brown } 30978d59e960SJed Brown *cfltime = ts->cfltime; 30988d59e960SJed Brown PetscFunctionReturn(0); 30998d59e960SJed Brown } 31008d59e960SJed Brown 31018d59e960SJed Brown #undef __FUNCT__ 3102d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3103d6ebe24aSShri Abhyankar /*@ 3104d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3105d6ebe24aSShri Abhyankar 3106d6ebe24aSShri Abhyankar Input Parameters: 3107d6ebe24aSShri Abhyankar . ts - the TS context. 3108d6ebe24aSShri Abhyankar . xl - lower bound. 3109d6ebe24aSShri Abhyankar . xu - upper bound. 3110d6ebe24aSShri Abhyankar 3111d6ebe24aSShri Abhyankar Notes: 3112d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 3113d6ebe24aSShri Abhyankar SNES_VI_INF and SNES_VI_NINF respectively during SNESSetUp(). 3114d6ebe24aSShri Abhyankar 31152bd2b0e6SSatish Balay Level: advanced 31162bd2b0e6SSatish Balay 3117d6ebe24aSShri Abhyankar @*/ 3118d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3119d6ebe24aSShri Abhyankar { 3120d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3121d6ebe24aSShri Abhyankar SNES snes; 3122d6ebe24aSShri Abhyankar 3123d6ebe24aSShri Abhyankar PetscFunctionBegin; 3124d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3125d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3126d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3127d6ebe24aSShri Abhyankar } 3128d6ebe24aSShri Abhyankar 3129325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3130c6db04a5SJed Brown #include <mex.h> 3131325fc9f4SBarry Smith 3132325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3133325fc9f4SBarry Smith 3134325fc9f4SBarry Smith #undef __FUNCT__ 3135325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3136325fc9f4SBarry Smith /* 3137325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3138325fc9f4SBarry Smith TSSetFunctionMatlab(). 3139325fc9f4SBarry Smith 3140325fc9f4SBarry Smith Collective on TS 3141325fc9f4SBarry Smith 3142325fc9f4SBarry Smith Input Parameters: 3143325fc9f4SBarry Smith + snes - the TS context 3144325fc9f4SBarry Smith - x - input vector 3145325fc9f4SBarry Smith 3146325fc9f4SBarry Smith Output Parameter: 3147325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3148325fc9f4SBarry Smith 3149325fc9f4SBarry Smith Notes: 3150325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3151325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3152325fc9f4SBarry Smith themselves. 3153325fc9f4SBarry Smith 3154325fc9f4SBarry Smith Level: developer 3155325fc9f4SBarry Smith 3156325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3157325fc9f4SBarry Smith 3158325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3159325fc9f4SBarry Smith */ 31607087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 3161325fc9f4SBarry Smith { 3162325fc9f4SBarry Smith PetscErrorCode ierr; 3163325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3164325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3165325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3166325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3167325fc9f4SBarry Smith 3168325fc9f4SBarry Smith PetscFunctionBegin; 3169325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3170325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3171325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 3172325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3173325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 3174325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3175325fc9f4SBarry Smith 3176325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3177325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3178880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 3179325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 3180325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3181325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3182325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3183325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3184325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3185325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3186325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3187325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3188325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3189325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3190325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3191325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3192325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3193325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3194325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3195325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3196325fc9f4SBarry Smith PetscFunctionReturn(0); 3197325fc9f4SBarry Smith } 3198325fc9f4SBarry Smith 3199325fc9f4SBarry Smith 3200325fc9f4SBarry Smith #undef __FUNCT__ 3201325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3202325fc9f4SBarry Smith /* 3203325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3204325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3205e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3206325fc9f4SBarry Smith 3207325fc9f4SBarry Smith Logically Collective on TS 3208325fc9f4SBarry Smith 3209325fc9f4SBarry Smith Input Parameters: 3210325fc9f4SBarry Smith + ts - the TS context 3211325fc9f4SBarry Smith - func - function evaluation routine 3212325fc9f4SBarry Smith 3213325fc9f4SBarry Smith Calling sequence of func: 3214325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 3215325fc9f4SBarry Smith 3216325fc9f4SBarry Smith Level: beginner 3217325fc9f4SBarry Smith 3218325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3219325fc9f4SBarry Smith 3220325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3221325fc9f4SBarry Smith */ 3222cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3223325fc9f4SBarry Smith { 3224325fc9f4SBarry Smith PetscErrorCode ierr; 3225325fc9f4SBarry Smith TSMatlabContext *sctx; 3226325fc9f4SBarry Smith 3227325fc9f4SBarry Smith PetscFunctionBegin; 3228325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3229325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3230325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3231325fc9f4SBarry Smith /* 3232325fc9f4SBarry Smith This should work, but it doesn't 3233325fc9f4SBarry Smith sctx->ctx = ctx; 3234325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3235325fc9f4SBarry Smith */ 3236325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3237cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3238325fc9f4SBarry Smith PetscFunctionReturn(0); 3239325fc9f4SBarry Smith } 3240325fc9f4SBarry Smith 3241325fc9f4SBarry Smith #undef __FUNCT__ 3242325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 3243325fc9f4SBarry Smith /* 3244325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 3245325fc9f4SBarry Smith TSSetJacobianMatlab(). 3246325fc9f4SBarry Smith 3247325fc9f4SBarry Smith Collective on TS 3248325fc9f4SBarry Smith 3249325fc9f4SBarry Smith Input Parameters: 3250cdcf91faSSean Farley + ts - the TS context 3251325fc9f4SBarry Smith . x - input vector 3252325fc9f4SBarry Smith . A, B - the matrices 3253325fc9f4SBarry Smith - ctx - user context 3254325fc9f4SBarry Smith 3255325fc9f4SBarry Smith Output Parameter: 3256325fc9f4SBarry Smith . flag - structure of the matrix 3257325fc9f4SBarry Smith 3258325fc9f4SBarry Smith Level: developer 3259325fc9f4SBarry Smith 3260325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3261325fc9f4SBarry Smith 3262325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3263325fc9f4SBarry Smith @*/ 3264cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3265325fc9f4SBarry Smith { 3266325fc9f4SBarry Smith PetscErrorCode ierr; 3267325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3268325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 3269325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 3270325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 3271325fc9f4SBarry Smith 3272325fc9f4SBarry Smith PetscFunctionBegin; 3273cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3274325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3275325fc9f4SBarry Smith 3276325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 3277325fc9f4SBarry Smith 3278cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3279325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3280325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 3281325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 3282325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 3283325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3284325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 3285325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3286325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3287325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 3288325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 3289325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 3290325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 3291325fc9f4SBarry Smith prhs[8] = sctx->ctx; 3292325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 3293325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3294325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 3295325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3296325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3297325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3298325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3299325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3300325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3301325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 3302325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 3303325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3304325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 3305325fc9f4SBarry Smith PetscFunctionReturn(0); 3306325fc9f4SBarry Smith } 3307325fc9f4SBarry Smith 3308325fc9f4SBarry Smith 3309325fc9f4SBarry Smith #undef __FUNCT__ 3310325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 3311325fc9f4SBarry Smith /* 3312325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 3313e3c5b3baSBarry 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 3314325fc9f4SBarry Smith 3315325fc9f4SBarry Smith Logically Collective on TS 3316325fc9f4SBarry Smith 3317325fc9f4SBarry Smith Input Parameters: 3318cdcf91faSSean Farley + ts - the TS context 3319325fc9f4SBarry Smith . A,B - Jacobian matrices 3320325fc9f4SBarry Smith . func - function evaluation routine 3321325fc9f4SBarry Smith - ctx - user context 3322325fc9f4SBarry Smith 3323325fc9f4SBarry Smith Calling sequence of func: 3324cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 3325325fc9f4SBarry Smith 3326325fc9f4SBarry Smith 3327325fc9f4SBarry Smith Level: developer 3328325fc9f4SBarry Smith 3329325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3330325fc9f4SBarry Smith 3331325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3332325fc9f4SBarry Smith */ 3333cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 3334325fc9f4SBarry Smith { 3335325fc9f4SBarry Smith PetscErrorCode ierr; 3336325fc9f4SBarry Smith TSMatlabContext *sctx; 3337325fc9f4SBarry Smith 3338325fc9f4SBarry Smith PetscFunctionBegin; 3339325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3340325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3341325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3342325fc9f4SBarry Smith /* 3343325fc9f4SBarry Smith This should work, but it doesn't 3344325fc9f4SBarry Smith sctx->ctx = ctx; 3345325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3346325fc9f4SBarry Smith */ 3347325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3348cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 3349325fc9f4SBarry Smith PetscFunctionReturn(0); 3350325fc9f4SBarry Smith } 3351325fc9f4SBarry Smith 3352b5b1a830SBarry Smith #undef __FUNCT__ 3353b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 3354b5b1a830SBarry Smith /* 3355b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 3356b5b1a830SBarry Smith 3357b5b1a830SBarry Smith Collective on TS 3358b5b1a830SBarry Smith 3359b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3360b5b1a830SBarry Smith @*/ 3361cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 3362b5b1a830SBarry Smith { 3363b5b1a830SBarry Smith PetscErrorCode ierr; 3364b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3365a530c242SBarry Smith int nlhs = 1,nrhs = 6; 3366b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 3367b5b1a830SBarry Smith long long int lx = 0,ls = 0; 3368b5b1a830SBarry Smith 3369b5b1a830SBarry Smith PetscFunctionBegin; 3370cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3371b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3372b5b1a830SBarry Smith 3373cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3374b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3375b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3376b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 3377b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 3378b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 3379b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 3380b5b1a830SBarry Smith prhs[5] = sctx->ctx; 3381b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 3382b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3383b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 3384b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 3385b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 3386b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 3387b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 3388b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 3389b5b1a830SBarry Smith PetscFunctionReturn(0); 3390b5b1a830SBarry Smith } 3391b5b1a830SBarry Smith 3392b5b1a830SBarry Smith 3393b5b1a830SBarry Smith #undef __FUNCT__ 3394b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 3395b5b1a830SBarry Smith /* 3396b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 3397b5b1a830SBarry Smith 3398b5b1a830SBarry Smith Level: developer 3399b5b1a830SBarry Smith 3400b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 3401b5b1a830SBarry Smith 3402b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3403b5b1a830SBarry Smith */ 3404cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 3405b5b1a830SBarry Smith { 3406b5b1a830SBarry Smith PetscErrorCode ierr; 3407b5b1a830SBarry Smith TSMatlabContext *sctx; 3408b5b1a830SBarry Smith 3409b5b1a830SBarry Smith PetscFunctionBegin; 3410b5b1a830SBarry Smith /* currently sctx is memory bleed */ 3411b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3412b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3413b5b1a830SBarry Smith /* 3414b5b1a830SBarry Smith This should work, but it doesn't 3415b5b1a830SBarry Smith sctx->ctx = ctx; 3416b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3417b5b1a830SBarry Smith */ 3418b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3419cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3420b5b1a830SBarry Smith PetscFunctionReturn(0); 3421b5b1a830SBarry Smith } 3422325fc9f4SBarry Smith #endif 3423