163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 4d763cef2SBarry Smith 5d5ba7fb7SMatthew Knepley /* Logging support */ 6d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 7166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 8d405a339SMatthew Knepley 949354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1049354f04SShri Abhyankar 114a2ae208SSatish Balay #undef __FUNCT__ 12bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 13bdad233fSMatthew Knepley /* 14bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 15bdad233fSMatthew Knepley 16bdad233fSMatthew Knepley Collective on TS 17bdad233fSMatthew Knepley 18bdad233fSMatthew Knepley Input Parameter: 19bdad233fSMatthew Knepley . ts - The ts 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley Level: intermediate 22bdad233fSMatthew Knepley 23bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 24bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 25bdad233fSMatthew Knepley */ 266849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 27bdad233fSMatthew Knepley { 28ace3abfcSBarry Smith PetscBool opt; 292fc52814SBarry Smith const char *defaultType; 30bdad233fSMatthew Knepley char typeName[256]; 31dfbe8321SBarry Smith PetscErrorCode ierr; 32bdad233fSMatthew Knepley 33bdad233fSMatthew Knepley PetscFunctionBegin; 347adad957SLisandro Dalcin if (((PetscObject)ts)->type_name) { 357adad957SLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 36bdad233fSMatthew Knepley } else { 379596e0b4SJed Brown defaultType = TSEULER; 38bdad233fSMatthew Knepley } 39bdad233fSMatthew Knepley 40cce0b1b2SLisandro Dalcin if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 41bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 42a7cc72afSBarry Smith if (opt) { 43bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 44bdad233fSMatthew Knepley } else { 45bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 46bdad233fSMatthew Knepley } 47bdad233fSMatthew Knepley PetscFunctionReturn(0); 48bdad233fSMatthew Knepley } 49bdad233fSMatthew Knepley 50bdad233fSMatthew Knepley #undef __FUNCT__ 51bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 52bdad233fSMatthew Knepley /*@ 53bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 54bdad233fSMatthew Knepley 55bdad233fSMatthew Knepley Collective on TS 56bdad233fSMatthew Knepley 57bdad233fSMatthew Knepley Input Parameter: 58bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 59bdad233fSMatthew Knepley 60bdad233fSMatthew Knepley Options Database Keys: 614d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 62bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 633bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 64bdad233fSMatthew Knepley . -ts_dt dt - initial time step 65bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 66de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 67de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 68de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 69de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 70de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 71de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 72de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 73de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 74de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 75de06c3feSJed Brown . -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file 76de06c3feSJed Brown - -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 77bdad233fSMatthew Knepley 78bdad233fSMatthew Knepley Level: beginner 79bdad233fSMatthew Knepley 80bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 81bdad233fSMatthew Knepley 82a313700dSBarry Smith .seealso: TSGetType() 83bdad233fSMatthew Knepley @*/ 847087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 85bdad233fSMatthew Knepley { 86ace3abfcSBarry Smith PetscBool opt,flg; 87dfbe8321SBarry Smith PetscErrorCode ierr; 88649052a6SBarry Smith PetscViewer monviewer; 89eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 90089b2837SJed Brown SNES snes; 911c3436cfSJed Brown TSAdapt adapt; 9231748224SBarry Smith PetscReal time_step; 9349354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 94d1212d36SBarry Smith char dir[16]; 95bdad233fSMatthew Knepley 96bdad233fSMatthew Knepley PetscFunctionBegin; 970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 983194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 99a43b19c4SJed Brown /* Handle TS type options */ 100a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 101bdad233fSMatthew Knepley 102bdad233fSMatthew Knepley /* Handle generic TS options */ 103bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 1043bca7d26SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 105d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 10631748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 10731748224SBarry Smith if (flg) { 10831748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 10931748224SBarry Smith } 11049354f04SShri Abhyankar ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr); 11149354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 112cef5090cSJed Brown ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr); 113cef5090cSJed Brown ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr); 114cef5090cSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 1151c3436cfSJed Brown ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 1161c3436cfSJed Brown ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 117bdad233fSMatthew Knepley 118bdad233fSMatthew Knepley /* Monitor options */ 119a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 120eabae89aSBarry Smith if (flg) { 121649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 122649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 123bdad233fSMatthew Knepley } 1245180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1255180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1265180491cSLisandro Dalcin 1274f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 128a7cc72afSBarry Smith if (opt) { 1290b039ecaSBarry Smith TSMonitorLGCtx ctx; 1303923b477SBarry Smith PetscInt howoften = 1; 131a80ad3e0SBarry Smith 1324f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1331d1e9da1SBarry Smith ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1344f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 135b3603a34SBarry Smith } 1364f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 137b3603a34SBarry Smith if (opt) { 1380b039ecaSBarry Smith TSMonitorLGCtx ctx; 1393923b477SBarry Smith PetscInt howoften = 1; 140b3603a34SBarry Smith 1414f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1423923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1434f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 144bdad233fSMatthew Knepley } 1454f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 146ef20d060SBarry Smith if (opt) { 1470b039ecaSBarry Smith TSMonitorLGCtx ctx; 1483923b477SBarry Smith PetscInt howoften = 1; 149ef20d060SBarry Smith 1504f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1513923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1524f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 153ef20d060SBarry Smith } 154201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 155201da799SBarry Smith if (opt) { 156201da799SBarry Smith TSMonitorLGCtx ctx; 157201da799SBarry Smith PetscInt howoften = 1; 158201da799SBarry Smith 159201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 160201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 161201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 162201da799SBarry Smith } 163201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 164201da799SBarry Smith if (opt) { 165201da799SBarry Smith TSMonitorLGCtx ctx; 166201da799SBarry Smith PetscInt howoften = 1; 167201da799SBarry Smith 168201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 169201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 170201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171201da799SBarry Smith } 1728189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1738189c53fSBarry Smith if (opt) { 1748189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1758189c53fSBarry Smith PetscInt howoften = 1; 1768189c53fSBarry Smith 1778189c53fSBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1788189c53fSBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1798189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1808189c53fSBarry Smith } 181ef20d060SBarry Smith opt = PETSC_FALSE; 1820dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 183a7cc72afSBarry Smith if (opt) { 18483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 18583a4ac43SBarry Smith PetscInt howoften = 1; 186a80ad3e0SBarry Smith 18783a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 18883a4ac43SBarry Smith ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 18983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 190bdad233fSMatthew Knepley } 191fb1732b5SBarry Smith opt = PETSC_FALSE; 1920dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 1933a471f94SBarry Smith if (opt) { 19483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 19583a4ac43SBarry Smith PetscInt howoften = 1; 1963a471f94SBarry Smith 19783a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 19883a4ac43SBarry Smith ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 19983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2003a471f94SBarry Smith } 2013a471f94SBarry Smith opt = PETSC_FALSE; 202de06c3feSJed Brown ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 203fb1732b5SBarry Smith if (flg) { 204fb1732b5SBarry Smith PetscViewer ctx; 205fb1732b5SBarry Smith if (monfilename[0]) { 206fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 207fb1732b5SBarry Smith } else { 208fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 209fb1732b5SBarry Smith } 210fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 211fb1732b5SBarry Smith } 212ed81e22dSJed Brown opt = PETSC_FALSE; 213de06c3feSJed Brown ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 214ed81e22dSJed Brown if (flg) { 215ed81e22dSJed Brown const char *ptr,*ptr2; 216ed81e22dSJed Brown char *filetemplate; 217de06c3feSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 218ed81e22dSJed Brown /* Do some cursory validation of the input. */ 219ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 220de06c3feSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 221ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 222ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 223de06c3feSJed Brown if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts"); 224ed81e22dSJed Brown if (ptr2) break; 225ed81e22dSJed Brown } 226ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 227ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 228ed81e22dSJed Brown } 229bdad233fSMatthew Knepley 230d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 231d1212d36SBarry Smith if (flg) { 232d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 233d1212d36SBarry Smith int ray = 0; 234d1212d36SBarry Smith DMDADirection ddir; 235d1212d36SBarry Smith DM da; 236d1212d36SBarry Smith PetscMPIInt rank; 237d1212d36SBarry Smith 238d1212d36SBarry Smith if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 239d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 240d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 241d1212d36SBarry Smith else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 242d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 243d1212d36SBarry Smith 244d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 245d1212d36SBarry Smith ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr); 246d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 247d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 248d1212d36SBarry Smith ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr); 249d1212d36SBarry Smith if (!rank) { 250d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 251d1212d36SBarry Smith } 252d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 253d1212d36SBarry Smith } 254d1212d36SBarry Smith 255ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr); 2561c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 2571c3436cfSJed Brown 258d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 259d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 260d52bd9f3SBarry Smith 261bdad233fSMatthew Knepley /* Handle specific TS options */ 262abc0a331SBarry Smith if (ts->ops->setfromoptions) { 263bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 264bdad233fSMatthew Knepley } 2655d973c19SBarry Smith 2665d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2675d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 268bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 269bdad233fSMatthew Knepley PetscFunctionReturn(0); 270bdad233fSMatthew Knepley } 271bdad233fSMatthew Knepley 272bdad233fSMatthew Knepley #undef __FUNCT__ 273cdcf91faSSean Farley #undef __FUNCT__ 2744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 275a7a1495cSBarry Smith /*@ 2768c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 277a7a1495cSBarry Smith set with TSSetRHSJacobian(). 278a7a1495cSBarry Smith 279a7a1495cSBarry Smith Collective on TS and Vec 280a7a1495cSBarry Smith 281a7a1495cSBarry Smith Input Parameters: 282316643e7SJed Brown + ts - the TS context 283a7a1495cSBarry Smith . t - current timestep 2840910c330SBarry Smith - U - input vector 285a7a1495cSBarry Smith 286a7a1495cSBarry Smith Output Parameters: 287a7a1495cSBarry Smith + A - Jacobian matrix 288a7a1495cSBarry Smith . B - optional preconditioning matrix 289a7a1495cSBarry Smith - flag - flag indicating matrix structure 290a7a1495cSBarry Smith 291a7a1495cSBarry Smith Notes: 292a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 293a7a1495cSBarry Smith is used internally within the nonlinear solvers. 294a7a1495cSBarry Smith 29594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 296a7a1495cSBarry Smith flag parameter. 297a7a1495cSBarry Smith 298a7a1495cSBarry Smith Level: developer 299a7a1495cSBarry Smith 300a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 301a7a1495cSBarry Smith 30294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 303a7a1495cSBarry Smith @*/ 3040910c330SBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg) 305a7a1495cSBarry Smith { 306dfbe8321SBarry Smith PetscErrorCode ierr; 3070910c330SBarry Smith PetscInt Ustate; 30824989b8cSPeter Brune DM dm; 309942e3340SBarry Smith DMTS tsdm; 31024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 31124989b8cSPeter Brune void *ctx; 31224989b8cSPeter Brune TSIJacobian ijacobianfunc; 313a7a1495cSBarry Smith 314a7a1495cSBarry Smith PetscFunctionBegin; 3150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3160910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3170910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 31824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 319942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 32024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 32124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 3220910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 3230910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 3240e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 3250e4ef248SJed Brown PetscFunctionReturn(0); 326f8ede8e7SJed Brown } 327d90be118SSean Farley 32824989b8cSPeter Brune if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 329d90be118SSean Farley 33024989b8cSPeter Brune if (rhsjacobianfunc) { 3310910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 332a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 333a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 3340910c330SBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr); 335a7a1495cSBarry Smith PetscStackPop; 3360910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 337a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 3380700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 3390700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 340ef66eb69SBarry Smith } else { 341214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 342214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 343214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 344ef66eb69SBarry Smith } 3450e4ef248SJed Brown ts->rhsjacobian.time = t; 3460910c330SBarry Smith ts->rhsjacobian.X = U; 3470910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 3480e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 349a7a1495cSBarry Smith PetscFunctionReturn(0); 350a7a1495cSBarry Smith } 351a7a1495cSBarry Smith 3524a2ae208SSatish Balay #undef __FUNCT__ 3534a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 354316643e7SJed Brown /*@ 355d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 356d763cef2SBarry Smith 357316643e7SJed Brown Collective on TS and Vec 358316643e7SJed Brown 359316643e7SJed Brown Input Parameters: 360316643e7SJed Brown + ts - the TS context 361316643e7SJed Brown . t - current time 3620910c330SBarry Smith - U - state vector 363316643e7SJed Brown 364316643e7SJed Brown Output Parameter: 365316643e7SJed Brown . y - right hand side 366316643e7SJed Brown 367316643e7SJed Brown Note: 368316643e7SJed Brown Most users should not need to explicitly call this routine, as it 369316643e7SJed Brown is used internally within the nonlinear solvers. 370316643e7SJed Brown 371316643e7SJed Brown Level: developer 372316643e7SJed Brown 373316643e7SJed Brown .keywords: TS, compute 374316643e7SJed Brown 375316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 376316643e7SJed Brown @*/ 3770910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 378d763cef2SBarry Smith { 379dfbe8321SBarry Smith PetscErrorCode ierr; 38024989b8cSPeter Brune TSRHSFunction rhsfunction; 38124989b8cSPeter Brune TSIFunction ifunction; 38224989b8cSPeter Brune void *ctx; 38324989b8cSPeter Brune DM dm; 38424989b8cSPeter Brune 385d763cef2SBarry Smith PetscFunctionBegin; 3860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3870910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3880700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 38924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 39024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 39124989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 392d763cef2SBarry Smith 39324989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 394d763cef2SBarry Smith 3950910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 39624989b8cSPeter Brune if (rhsfunction) { 397d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 3980910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 399d763cef2SBarry Smith PetscStackPop; 400214bc6a2SJed Brown } else { 401214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 402b2cd27e8SJed Brown } 40344a41b28SSean Farley 4040910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 405d763cef2SBarry Smith PetscFunctionReturn(0); 406d763cef2SBarry Smith } 407d763cef2SBarry Smith 4084a2ae208SSatish Balay #undef __FUNCT__ 409ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 410ef20d060SBarry Smith /*@ 411ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 412ef20d060SBarry Smith 413ef20d060SBarry Smith Collective on TS and Vec 414ef20d060SBarry Smith 415ef20d060SBarry Smith Input Parameters: 416ef20d060SBarry Smith + ts - the TS context 417ef20d060SBarry Smith - t - current time 418ef20d060SBarry Smith 419ef20d060SBarry Smith Output Parameter: 4200910c330SBarry Smith . U - the solution 421ef20d060SBarry Smith 422ef20d060SBarry Smith Note: 423ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 424ef20d060SBarry Smith is used internally within the nonlinear solvers. 425ef20d060SBarry Smith 426ef20d060SBarry Smith Level: developer 427ef20d060SBarry Smith 428ef20d060SBarry Smith .keywords: TS, compute 429ef20d060SBarry Smith 430abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 431ef20d060SBarry Smith @*/ 4320910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 433ef20d060SBarry Smith { 434ef20d060SBarry Smith PetscErrorCode ierr; 435ef20d060SBarry Smith TSSolutionFunction solutionfunction; 436ef20d060SBarry Smith void *ctx; 437ef20d060SBarry Smith DM dm; 438ef20d060SBarry Smith 439ef20d060SBarry Smith PetscFunctionBegin; 440ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4410910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 442ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 443ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 444ef20d060SBarry Smith 445ef20d060SBarry Smith if (solutionfunction) { 446ef20d060SBarry Smith PetscStackPush("TS user right-hand-side function"); 4470910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 448ef20d060SBarry Smith PetscStackPop; 449ef20d060SBarry Smith } 450ef20d060SBarry Smith PetscFunctionReturn(0); 451ef20d060SBarry Smith } 452ef20d060SBarry Smith 453ef20d060SBarry Smith #undef __FUNCT__ 454214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 455214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 456214bc6a2SJed Brown { 4572dd45cf8SJed Brown Vec F; 458214bc6a2SJed Brown PetscErrorCode ierr; 459214bc6a2SJed Brown 460214bc6a2SJed Brown PetscFunctionBegin; 4610da9a4f0SJed Brown *Frhs = PETSC_NULL; 4622dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 463214bc6a2SJed Brown if (!ts->Frhs) { 4642dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 465214bc6a2SJed Brown } 466214bc6a2SJed Brown *Frhs = ts->Frhs; 467214bc6a2SJed Brown PetscFunctionReturn(0); 468214bc6a2SJed Brown } 469214bc6a2SJed Brown 470214bc6a2SJed Brown #undef __FUNCT__ 471214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 472214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 473214bc6a2SJed Brown { 474214bc6a2SJed Brown Mat A,B; 4752dd45cf8SJed Brown PetscErrorCode ierr; 476214bc6a2SJed Brown 477214bc6a2SJed Brown PetscFunctionBegin; 478214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 479214bc6a2SJed Brown if (Arhs) { 480214bc6a2SJed Brown if (!ts->Arhs) { 481214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 482214bc6a2SJed Brown } 483214bc6a2SJed Brown *Arhs = ts->Arhs; 484214bc6a2SJed Brown } 485214bc6a2SJed Brown if (Brhs) { 486214bc6a2SJed Brown if (!ts->Brhs) { 487214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 488214bc6a2SJed Brown } 489214bc6a2SJed Brown *Brhs = ts->Brhs; 490214bc6a2SJed Brown } 491214bc6a2SJed Brown PetscFunctionReturn(0); 492214bc6a2SJed Brown } 493214bc6a2SJed Brown 494214bc6a2SJed Brown #undef __FUNCT__ 495316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 496316643e7SJed Brown /*@ 4970910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 498316643e7SJed Brown 499316643e7SJed Brown Collective on TS and Vec 500316643e7SJed Brown 501316643e7SJed Brown Input Parameters: 502316643e7SJed Brown + ts - the TS context 503316643e7SJed Brown . t - current time 5040910c330SBarry Smith . U - state vector 5050910c330SBarry Smith . Udot - time derivative of state vector 506214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 507316643e7SJed Brown 508316643e7SJed Brown Output Parameter: 509316643e7SJed Brown . Y - right hand side 510316643e7SJed Brown 511316643e7SJed Brown Note: 512316643e7SJed Brown Most users should not need to explicitly call this routine, as it 513316643e7SJed Brown is used internally within the nonlinear solvers. 514316643e7SJed Brown 515316643e7SJed Brown If the user did did not write their equations in implicit form, this 516316643e7SJed Brown function recasts them in implicit form. 517316643e7SJed Brown 518316643e7SJed Brown Level: developer 519316643e7SJed Brown 520316643e7SJed Brown .keywords: TS, compute 521316643e7SJed Brown 522316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 523316643e7SJed Brown @*/ 5240910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 525316643e7SJed Brown { 526316643e7SJed Brown PetscErrorCode ierr; 52724989b8cSPeter Brune TSIFunction ifunction; 52824989b8cSPeter Brune TSRHSFunction rhsfunction; 52924989b8cSPeter Brune void *ctx; 53024989b8cSPeter Brune DM dm; 531316643e7SJed Brown 532316643e7SJed Brown PetscFunctionBegin; 5330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5340910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5350910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 5360700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 537316643e7SJed Brown 53824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 53924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 54024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 54124989b8cSPeter Brune 54224989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 543d90be118SSean Farley 5440910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 54524989b8cSPeter Brune if (ifunction) { 546316643e7SJed Brown PetscStackPush("TS user implicit function"); 5470910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 548316643e7SJed Brown PetscStackPop; 549214bc6a2SJed Brown } 550214bc6a2SJed Brown if (imex) { 55124989b8cSPeter Brune if (!ifunction) { 5520910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 5532dd45cf8SJed Brown } 55424989b8cSPeter Brune } else if (rhsfunction) { 55524989b8cSPeter Brune if (ifunction) { 556214bc6a2SJed Brown Vec Frhs; 557214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 5580910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 559214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 5602dd45cf8SJed Brown } else { 5610910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 5620910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 563316643e7SJed Brown } 5644a6899ffSJed Brown } 5650910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 566316643e7SJed Brown PetscFunctionReturn(0); 567316643e7SJed Brown } 568316643e7SJed Brown 569316643e7SJed Brown #undef __FUNCT__ 570316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 571316643e7SJed Brown /*@ 572316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 573316643e7SJed Brown 574316643e7SJed Brown Collective on TS and Vec 575316643e7SJed Brown 576316643e7SJed Brown Input 577316643e7SJed Brown Input Parameters: 578316643e7SJed Brown + ts - the TS context 579316643e7SJed Brown . t - current timestep 5800910c330SBarry Smith . U - state vector 5810910c330SBarry Smith . Udot - time derivative of state vector 582214bc6a2SJed Brown . shift - shift to apply, see note below 583214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 584316643e7SJed Brown 585316643e7SJed Brown Output Parameters: 586316643e7SJed Brown + A - Jacobian matrix 587316643e7SJed Brown . B - optional preconditioning matrix 588316643e7SJed Brown - flag - flag indicating matrix structure 589316643e7SJed Brown 590316643e7SJed Brown Notes: 5910910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 592316643e7SJed Brown 5930910c330SBarry Smith dF/dU + shift*dF/dUdot 594316643e7SJed Brown 595316643e7SJed Brown Most users should not need to explicitly call this routine, as it 596316643e7SJed Brown is used internally within the nonlinear solvers. 597316643e7SJed Brown 598316643e7SJed Brown Level: developer 599316643e7SJed Brown 600316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 601316643e7SJed Brown 602316643e7SJed Brown .seealso: TSSetIJacobian() 60363495f91SJed Brown @*/ 6040910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 605316643e7SJed Brown { 6060910c330SBarry Smith PetscInt Ustate, Udotstate; 607316643e7SJed Brown PetscErrorCode ierr; 60824989b8cSPeter Brune TSIJacobian ijacobian; 60924989b8cSPeter Brune TSRHSJacobian rhsjacobian; 61024989b8cSPeter Brune DM dm; 61124989b8cSPeter Brune void *ctx; 612316643e7SJed Brown 613316643e7SJed Brown PetscFunctionBegin; 61424989b8cSPeter Brune 6150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6160910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6170910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 618316643e7SJed Brown PetscValidPointer(A,6); 6190700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 620316643e7SJed Brown PetscValidPointer(B,7); 6210700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 622316643e7SJed Brown PetscValidPointer(flg,8); 62324989b8cSPeter Brune 62424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 62524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 62624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 62724989b8cSPeter Brune 6280910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 6290910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr); 6300910c330SBarry Smith if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) { 6310026cea9SSean Farley *flg = ts->ijacobian.mstructure; 6320026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 6330026cea9SSean Farley PetscFunctionReturn(0); 6340026cea9SSean Farley } 635316643e7SJed Brown 63624989b8cSPeter Brune if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 637316643e7SJed Brown 6384e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 6390910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 64024989b8cSPeter Brune if (ijacobian) { 6412dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 642316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 6430910c330SBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr); 644316643e7SJed Brown PetscStackPop; 645214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 646214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 647214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 6484a6899ffSJed Brown } 649214bc6a2SJed Brown if (imex) { 650b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 651214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 6522dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 653214bc6a2SJed Brown if (*A != *B) { 654214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 655214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 656214bc6a2SJed Brown } 657214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 658214bc6a2SJed Brown } 659214bc6a2SJed Brown } else { 66024989b8cSPeter Brune if (!ijacobian) { 6610910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr); 662214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 663214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 664316643e7SJed Brown if (*A != *B) { 665316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 666316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 667316643e7SJed Brown } 66824989b8cSPeter Brune } else if (rhsjacobian) { 669214bc6a2SJed Brown Mat Arhs,Brhs; 670214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 671214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 6720910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 673214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 674214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 675214bc6a2SJed Brown if (*A != *B) { 676214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 677214bc6a2SJed Brown } 678214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 679214bc6a2SJed Brown } 680316643e7SJed Brown } 6810026cea9SSean Farley 6820026cea9SSean Farley ts->ijacobian.time = t; 6830910c330SBarry Smith ts->ijacobian.X = U; 6840910c330SBarry Smith ts->ijacobian.Xdot = Udot; 6850910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr); 6860910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 6870026cea9SSean Farley ts->ijacobian.shift = shift; 6880026cea9SSean Farley ts->ijacobian.imex = imex; 6890026cea9SSean Farley ts->ijacobian.mstructure = *flg; 6900910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 691316643e7SJed Brown PetscFunctionReturn(0); 692316643e7SJed Brown } 693316643e7SJed Brown 694316643e7SJed Brown #undef __FUNCT__ 6954a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 696d763cef2SBarry Smith /*@C 697d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 698b5abc632SBarry Smith where U_t = G(t,u). 699d763cef2SBarry Smith 7003f9fe445SBarry Smith Logically Collective on TS 701d763cef2SBarry Smith 702d763cef2SBarry Smith Input Parameters: 703d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 704ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 705d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 706d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 707d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 708d763cef2SBarry Smith 709d763cef2SBarry Smith Calling sequence of func: 71087828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 711d763cef2SBarry Smith 712d763cef2SBarry Smith + t - current timestep 713d763cef2SBarry Smith . u - input vector 714d763cef2SBarry Smith . F - function vector 715d763cef2SBarry Smith - ctx - [optional] user-defined function context 716d763cef2SBarry Smith 717d763cef2SBarry Smith Level: beginner 718d763cef2SBarry Smith 719d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 720d763cef2SBarry Smith 721d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 722d763cef2SBarry Smith @*/ 723089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 724d763cef2SBarry Smith { 725089b2837SJed Brown PetscErrorCode ierr; 726089b2837SJed Brown SNES snes; 727e856ceecSJed Brown Vec ralloc = PETSC_NULL; 72824989b8cSPeter Brune DM dm; 729d763cef2SBarry Smith 730089b2837SJed Brown PetscFunctionBegin; 7310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 732ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 73324989b8cSPeter Brune 73424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 73524989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 736089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 737e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 738e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 739e856ceecSJed Brown r = ralloc; 740e856ceecSJed Brown } 741089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 742e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 743d763cef2SBarry Smith PetscFunctionReturn(0); 744d763cef2SBarry Smith } 745d763cef2SBarry Smith 7464a2ae208SSatish Balay #undef __FUNCT__ 747ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 748ef20d060SBarry Smith /*@C 749abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 750ef20d060SBarry Smith 751ef20d060SBarry Smith Logically Collective on TS 752ef20d060SBarry Smith 753ef20d060SBarry Smith Input Parameters: 754ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 755ef20d060SBarry Smith . f - routine for evaluating the solution 756ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 757ef20d060SBarry Smith function evaluation routine (may be PETSC_NULL) 758ef20d060SBarry Smith 759ef20d060SBarry Smith Calling sequence of func: 760ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 761ef20d060SBarry Smith 762ef20d060SBarry Smith + t - current timestep 763ef20d060SBarry Smith . u - output vector 764ef20d060SBarry Smith - ctx - [optional] user-defined function context 765ef20d060SBarry Smith 766abd5a294SJed Brown Notes: 767abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 768abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 769abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 770abd5a294SJed Brown 7714f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 772abd5a294SJed Brown 773ef20d060SBarry Smith Level: beginner 774ef20d060SBarry Smith 775ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 776ef20d060SBarry Smith 777abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 778ef20d060SBarry Smith @*/ 779ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 780ef20d060SBarry Smith { 781ef20d060SBarry Smith PetscErrorCode ierr; 782ef20d060SBarry Smith DM dm; 783ef20d060SBarry Smith 784ef20d060SBarry Smith PetscFunctionBegin; 785ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 786ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 787ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 788ef20d060SBarry Smith PetscFunctionReturn(0); 789ef20d060SBarry Smith } 790ef20d060SBarry Smith 791ef20d060SBarry Smith #undef __FUNCT__ 7924a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 793d763cef2SBarry Smith /*@C 794d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 795b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 796d763cef2SBarry Smith 7973f9fe445SBarry Smith Logically Collective on TS 798d763cef2SBarry Smith 799d763cef2SBarry Smith Input Parameters: 800d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 801d763cef2SBarry Smith . A - Jacobian matrix 802d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 803d763cef2SBarry Smith . f - the Jacobian evaluation routine 804d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 805d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 806d763cef2SBarry Smith 807d763cef2SBarry Smith Calling sequence of func: 80887828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 809d763cef2SBarry Smith 810d763cef2SBarry Smith + t - current timestep 811d763cef2SBarry Smith . u - input vector 812d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 813d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 814d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 81594b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 816d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 817d763cef2SBarry Smith 818d763cef2SBarry Smith Notes: 81994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 820d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 821d763cef2SBarry Smith 822d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 823d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 82456335db2SHong Zhang completely new matrix structure (not just different matrix elements) 825d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 826d763cef2SBarry Smith throughout the global iterations. 827d763cef2SBarry Smith 828d763cef2SBarry Smith Level: beginner 829d763cef2SBarry Smith 830d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 831d763cef2SBarry Smith 832ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 833d763cef2SBarry Smith 834d763cef2SBarry Smith @*/ 835089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 836d763cef2SBarry Smith { 837277b19d0SLisandro Dalcin PetscErrorCode ierr; 838089b2837SJed Brown SNES snes; 83924989b8cSPeter Brune DM dm; 84024989b8cSPeter Brune TSIJacobian ijacobian; 841277b19d0SLisandro Dalcin 842d763cef2SBarry Smith PetscFunctionBegin; 8430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 844277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 845277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 846277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 847277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 848d763cef2SBarry Smith 84924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 85024989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 85124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 85224989b8cSPeter Brune 853089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 8545f659677SPeter Brune if (!ijacobian) { 855089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 8560e4ef248SJed Brown } 8570e4ef248SJed Brown if (A) { 8580e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8590e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 8600e4ef248SJed Brown ts->Arhs = A; 8610e4ef248SJed Brown } 8620e4ef248SJed Brown if (B) { 8630e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 8640e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 8650e4ef248SJed Brown ts->Brhs = B; 8660e4ef248SJed Brown } 867d763cef2SBarry Smith PetscFunctionReturn(0); 868d763cef2SBarry Smith } 869d763cef2SBarry Smith 870316643e7SJed Brown 871316643e7SJed Brown #undef __FUNCT__ 872316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 873316643e7SJed Brown /*@C 874b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 875316643e7SJed Brown 8763f9fe445SBarry Smith Logically Collective on TS 877316643e7SJed Brown 878316643e7SJed Brown Input Parameters: 879316643e7SJed Brown + ts - the TS context obtained from TSCreate() 880ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 881316643e7SJed Brown . f - the function evaluation routine 882316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 883316643e7SJed Brown 884316643e7SJed Brown Calling sequence of f: 885316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 886316643e7SJed Brown 887316643e7SJed Brown + t - time at step/stage being solved 888316643e7SJed Brown . u - state vector 889316643e7SJed Brown . u_t - time derivative of state vector 890316643e7SJed Brown . F - function vector 891316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 892316643e7SJed Brown 893316643e7SJed Brown Important: 894d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 895316643e7SJed Brown 896316643e7SJed Brown Level: beginner 897316643e7SJed Brown 898316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 899316643e7SJed Brown 900d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 901316643e7SJed Brown @*/ 902089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 903316643e7SJed Brown { 904089b2837SJed Brown PetscErrorCode ierr; 905089b2837SJed Brown SNES snes; 906e856ceecSJed Brown Vec resalloc = PETSC_NULL; 90724989b8cSPeter Brune DM dm; 908316643e7SJed Brown 909316643e7SJed Brown PetscFunctionBegin; 9100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 911ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 91224989b8cSPeter Brune 91324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 91424989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 91524989b8cSPeter Brune 916089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 917e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 918e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 919e856ceecSJed Brown res = resalloc; 920e856ceecSJed Brown } 921089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 922e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 92324989b8cSPeter Brune 924089b2837SJed Brown PetscFunctionReturn(0); 925089b2837SJed Brown } 926089b2837SJed Brown 927089b2837SJed Brown #undef __FUNCT__ 928089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 929089b2837SJed Brown /*@C 930089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 931089b2837SJed Brown 932089b2837SJed Brown Not Collective 933089b2837SJed Brown 934089b2837SJed Brown Input Parameter: 935089b2837SJed Brown . ts - the TS context 936089b2837SJed Brown 937089b2837SJed Brown Output Parameter: 938089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 939089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 940089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 941089b2837SJed Brown 942089b2837SJed Brown Level: advanced 943089b2837SJed Brown 944089b2837SJed Brown .keywords: TS, nonlinear, get, function 945089b2837SJed Brown 946089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 947089b2837SJed Brown @*/ 948089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 949089b2837SJed Brown { 950089b2837SJed Brown PetscErrorCode ierr; 951089b2837SJed Brown SNES snes; 95224989b8cSPeter Brune DM dm; 953089b2837SJed Brown 954089b2837SJed Brown PetscFunctionBegin; 955089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 956089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 957089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 95824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 95924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 960089b2837SJed Brown PetscFunctionReturn(0); 961089b2837SJed Brown } 962089b2837SJed Brown 963089b2837SJed Brown #undef __FUNCT__ 964089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 965089b2837SJed Brown /*@C 966089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 967089b2837SJed Brown 968089b2837SJed Brown Not Collective 969089b2837SJed Brown 970089b2837SJed Brown Input Parameter: 971089b2837SJed Brown . ts - the TS context 972089b2837SJed Brown 973089b2837SJed Brown Output Parameter: 974089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 975089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 976089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 977089b2837SJed Brown 978089b2837SJed Brown Level: advanced 979089b2837SJed Brown 980089b2837SJed Brown .keywords: TS, nonlinear, get, function 981089b2837SJed Brown 982089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 983089b2837SJed Brown @*/ 984089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 985089b2837SJed Brown { 986089b2837SJed Brown PetscErrorCode ierr; 987089b2837SJed Brown SNES snes; 98824989b8cSPeter Brune DM dm; 989089b2837SJed Brown 990089b2837SJed Brown PetscFunctionBegin; 991089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 992089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 993089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 99424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 99524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 996316643e7SJed Brown PetscFunctionReturn(0); 997316643e7SJed Brown } 998316643e7SJed Brown 999316643e7SJed Brown #undef __FUNCT__ 1000316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1001316643e7SJed Brown /*@C 1002a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1003a4f0a591SBarry Smith you provided with TSSetIFunction(). 1004316643e7SJed Brown 10053f9fe445SBarry Smith Logically Collective on TS 1006316643e7SJed Brown 1007316643e7SJed Brown Input Parameters: 1008316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1009316643e7SJed Brown . A - Jacobian matrix 1010316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 1011316643e7SJed Brown . f - the Jacobian evaluation routine 1012316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 1013316643e7SJed Brown 1014316643e7SJed Brown Calling sequence of f: 10151b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 1016316643e7SJed Brown 1017316643e7SJed Brown + t - time at step/stage being solved 10181b4a444bSJed Brown . U - state vector 10191b4a444bSJed Brown . U_t - time derivative of state vector 1020316643e7SJed Brown . a - shift 1021b5abc632SBarry Smith . A - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1022316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 1023316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 1024316643e7SJed Brown structure (same as flag in KSPSetOperators()) 1025316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1026316643e7SJed Brown 1027316643e7SJed Brown Notes: 1028316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 1029316643e7SJed Brown 1030a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1031b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1032a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1033a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1034a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1035a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1036a4f0a591SBarry Smith 1037316643e7SJed Brown Level: beginner 1038316643e7SJed Brown 1039316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1040316643e7SJed Brown 1041ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 1042316643e7SJed Brown 1043316643e7SJed Brown @*/ 10447087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 1045316643e7SJed Brown { 1046316643e7SJed Brown PetscErrorCode ierr; 1047089b2837SJed Brown SNES snes; 104824989b8cSPeter Brune DM dm; 1049316643e7SJed Brown 1050316643e7SJed Brown PetscFunctionBegin; 10510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10520700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 10530700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1054316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 1055316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 105624989b8cSPeter Brune 105724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 105824989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 105924989b8cSPeter Brune 1060089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1061089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1062316643e7SJed Brown PetscFunctionReturn(0); 1063316643e7SJed Brown } 1064316643e7SJed Brown 10654a2ae208SSatish Balay #undef __FUNCT__ 106655849f57SBarry Smith #define __FUNCT__ "TSLoad" 106755849f57SBarry Smith /*@C 106855849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 106955849f57SBarry Smith 107055849f57SBarry Smith Collective on PetscViewer 107155849f57SBarry Smith 107255849f57SBarry Smith Input Parameters: 107355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 107455849f57SBarry Smith some related function before a call to TSLoad(). 107555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 107655849f57SBarry Smith 107755849f57SBarry Smith Level: intermediate 107855849f57SBarry Smith 107955849f57SBarry Smith Notes: 108055849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 108155849f57SBarry Smith 108255849f57SBarry Smith Notes for advanced users: 108355849f57SBarry Smith Most users should not need to know the details of the binary storage 108455849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 108555849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 108655849f57SBarry Smith format is 108755849f57SBarry Smith .vb 108855849f57SBarry Smith has not yet been determined 108955849f57SBarry Smith .ve 109055849f57SBarry Smith 109155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 109255849f57SBarry Smith @*/ 1093f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 109455849f57SBarry Smith { 109555849f57SBarry Smith PetscErrorCode ierr; 109655849f57SBarry Smith PetscBool isbinary; 109755849f57SBarry Smith PetscInt classid; 109855849f57SBarry Smith char type[256]; 10992d53ad75SBarry Smith DMTS sdm; 1100ad6bc421SBarry Smith DM dm; 110155849f57SBarry Smith 110255849f57SBarry Smith PetscFunctionBegin; 1103f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 110455849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 110555849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 110655849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 110755849f57SBarry Smith 110855849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1109f2c2a1b9SBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file"); 111055849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1111f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1112f2c2a1b9SBarry Smith if (ts->ops->load) { 1113f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1114f2c2a1b9SBarry Smith } 1115ad6bc421SBarry Smith ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr); 1116ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1117ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1118f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1119f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 11202d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 11212d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 112255849f57SBarry Smith PetscFunctionReturn(0); 112355849f57SBarry Smith } 112455849f57SBarry Smith 112555849f57SBarry Smith #undef __FUNCT__ 11264a2ae208SSatish Balay #define __FUNCT__ "TSView" 11277e2c5f70SBarry Smith /*@C 1128d763cef2SBarry Smith TSView - Prints the TS data structure. 1129d763cef2SBarry Smith 11304c49b128SBarry Smith Collective on TS 1131d763cef2SBarry Smith 1132d763cef2SBarry Smith Input Parameters: 1133d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1134d763cef2SBarry Smith - viewer - visualization context 1135d763cef2SBarry Smith 1136d763cef2SBarry Smith Options Database Key: 1137d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1138d763cef2SBarry Smith 1139d763cef2SBarry Smith Notes: 1140d763cef2SBarry Smith The available visualization contexts include 1141b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1142b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1143d763cef2SBarry Smith output where only the first processor opens 1144d763cef2SBarry Smith the file. All other processors send their 1145d763cef2SBarry Smith data to the first processor to print. 1146d763cef2SBarry Smith 1147d763cef2SBarry Smith The user can open an alternative visualization context with 1148b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1149d763cef2SBarry Smith 1150d763cef2SBarry Smith Level: beginner 1151d763cef2SBarry Smith 1152d763cef2SBarry Smith .keywords: TS, timestep, view 1153d763cef2SBarry Smith 1154b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1155d763cef2SBarry Smith @*/ 11567087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1157d763cef2SBarry Smith { 1158dfbe8321SBarry Smith PetscErrorCode ierr; 115919fd82e9SBarry Smith TSType type; 11602b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 11612d53ad75SBarry Smith DMTS sdm; 1162d763cef2SBarry Smith 1163d763cef2SBarry Smith PetscFunctionBegin; 11640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11653050cee2SBarry Smith if (!viewer) { 11667adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 11673050cee2SBarry Smith } 11680700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1169c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1170fd16b177SBarry Smith 1171251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1172251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 117355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 11742b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 117532077d6dSBarry Smith if (iascii) { 1176317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 117777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1178a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1179d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 11805ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1181c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1182d763cef2SBarry Smith } 11835ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1184193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 11852d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 11862d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1187d52bd9f3SBarry Smith if (ts->ops->view) { 1188d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1189d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1190d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1191d52bd9f3SBarry Smith } 11920f5bd95cSBarry Smith } else if (isstring) { 1193a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1194b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 119555849f57SBarry Smith } else if (isbinary) { 119655849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 119755849f57SBarry Smith MPI_Comm comm; 119855849f57SBarry Smith PetscMPIInt rank; 119955849f57SBarry Smith char type[256]; 120055849f57SBarry Smith 120155849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 120255849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 120355849f57SBarry Smith if (!rank) { 120455849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 120555849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 120655849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 120755849f57SBarry Smith } 120855849f57SBarry Smith if (ts->ops->view) { 120955849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 121055849f57SBarry Smith } 1211f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1212f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 12132d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 12142d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 12152b0a91c0SBarry Smith } else if (isdraw) { 12162b0a91c0SBarry Smith PetscDraw draw; 12172b0a91c0SBarry Smith char str[36]; 121889fd9fafSBarry Smith PetscReal x,y,bottom,h; 12192b0a91c0SBarry Smith 12202b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 12212b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 12222b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 12232b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 122489fd9fafSBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr); 122589fd9fafSBarry Smith bottom = y - h; 12262b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 12272b0a91c0SBarry Smith if (ts->ops->view) { 12282b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 12292b0a91c0SBarry Smith } 12302b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1231d763cef2SBarry Smith } 1232b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1233251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1234b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1235d763cef2SBarry Smith PetscFunctionReturn(0); 1236d763cef2SBarry Smith } 1237d763cef2SBarry Smith 1238d763cef2SBarry Smith 12394a2ae208SSatish Balay #undef __FUNCT__ 12404a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1241b07ff414SBarry Smith /*@ 1242d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1243d763cef2SBarry Smith the timesteppers. 1244d763cef2SBarry Smith 12453f9fe445SBarry Smith Logically Collective on TS 1246d763cef2SBarry Smith 1247d763cef2SBarry Smith Input Parameters: 1248d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1249d763cef2SBarry Smith - usrP - optional user context 1250d763cef2SBarry Smith 1251d763cef2SBarry Smith Level: intermediate 1252d763cef2SBarry Smith 1253d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1254d763cef2SBarry Smith 1255d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1256d763cef2SBarry Smith @*/ 12577087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1258d763cef2SBarry Smith { 1259d763cef2SBarry Smith PetscFunctionBegin; 12600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1261d763cef2SBarry Smith ts->user = usrP; 1262d763cef2SBarry Smith PetscFunctionReturn(0); 1263d763cef2SBarry Smith } 1264d763cef2SBarry Smith 12654a2ae208SSatish Balay #undef __FUNCT__ 12664a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1267b07ff414SBarry Smith /*@ 1268d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1269d763cef2SBarry Smith timestepper. 1270d763cef2SBarry Smith 1271d763cef2SBarry Smith Not Collective 1272d763cef2SBarry Smith 1273d763cef2SBarry Smith Input Parameter: 1274d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1275d763cef2SBarry Smith 1276d763cef2SBarry Smith Output Parameter: 1277d763cef2SBarry Smith . usrP - user context 1278d763cef2SBarry Smith 1279d763cef2SBarry Smith Level: intermediate 1280d763cef2SBarry Smith 1281d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1282d763cef2SBarry Smith 1283d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1284d763cef2SBarry Smith @*/ 1285e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1286d763cef2SBarry Smith { 1287d763cef2SBarry Smith PetscFunctionBegin; 12880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1289e71120c6SJed Brown *(void**)usrP = ts->user; 1290d763cef2SBarry Smith PetscFunctionReturn(0); 1291d763cef2SBarry Smith } 1292d763cef2SBarry Smith 12934a2ae208SSatish Balay #undef __FUNCT__ 12944a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1295d763cef2SBarry Smith /*@ 1296b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1297d763cef2SBarry Smith 1298d763cef2SBarry Smith Not Collective 1299d763cef2SBarry Smith 1300d763cef2SBarry Smith Input Parameter: 1301d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1302d763cef2SBarry Smith 1303d763cef2SBarry Smith Output Parameter: 1304b8123daeSJed Brown . iter - number of steps completed so far 1305d763cef2SBarry Smith 1306d763cef2SBarry Smith Level: intermediate 1307d763cef2SBarry Smith 1308d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1309b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1310d763cef2SBarry Smith @*/ 13117087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1312d763cef2SBarry Smith { 1313d763cef2SBarry Smith PetscFunctionBegin; 13140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13154482741eSBarry Smith PetscValidIntPointer(iter,2); 1316d763cef2SBarry Smith *iter = ts->steps; 1317d763cef2SBarry Smith PetscFunctionReturn(0); 1318d763cef2SBarry Smith } 1319d763cef2SBarry Smith 13204a2ae208SSatish Balay #undef __FUNCT__ 13214a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1322d763cef2SBarry Smith /*@ 1323d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1324d763cef2SBarry Smith as well as the initial time. 1325d763cef2SBarry Smith 13263f9fe445SBarry Smith Logically Collective on TS 1327d763cef2SBarry Smith 1328d763cef2SBarry Smith Input Parameters: 1329d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1330d763cef2SBarry Smith . initial_time - the initial time 1331d763cef2SBarry Smith - time_step - the size of the timestep 1332d763cef2SBarry Smith 1333d763cef2SBarry Smith Level: intermediate 1334d763cef2SBarry Smith 1335d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1336d763cef2SBarry Smith 1337d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1338d763cef2SBarry Smith @*/ 13397087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1340d763cef2SBarry Smith { 1341e144a568SJed Brown PetscErrorCode ierr; 1342e144a568SJed Brown 1343d763cef2SBarry Smith PetscFunctionBegin; 13440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1345e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1346d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1347d763cef2SBarry Smith PetscFunctionReturn(0); 1348d763cef2SBarry Smith } 1349d763cef2SBarry Smith 13504a2ae208SSatish Balay #undef __FUNCT__ 13514a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1352d763cef2SBarry Smith /*@ 1353d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1354d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1355d763cef2SBarry Smith 13563f9fe445SBarry Smith Logically Collective on TS 1357d763cef2SBarry Smith 1358d763cef2SBarry Smith Input Parameters: 1359d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1360d763cef2SBarry Smith - time_step - the size of the timestep 1361d763cef2SBarry Smith 1362d763cef2SBarry Smith Level: intermediate 1363d763cef2SBarry Smith 1364d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1365d763cef2SBarry Smith 1366d763cef2SBarry Smith .keywords: TS, set, timestep 1367d763cef2SBarry Smith @*/ 13687087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1369d763cef2SBarry Smith { 1370d763cef2SBarry Smith PetscFunctionBegin; 13710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1372c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1373d763cef2SBarry Smith ts->time_step = time_step; 137431748224SBarry Smith ts->time_step_orig = time_step; 1375d763cef2SBarry Smith PetscFunctionReturn(0); 1376d763cef2SBarry Smith } 1377d763cef2SBarry Smith 13784a2ae208SSatish Balay #undef __FUNCT__ 1379a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1380a43b19c4SJed Brown /*@ 138149354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 138249354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 138349354f04SShri Abhyankar or just return at the final time TS computed. 1384a43b19c4SJed Brown 1385a43b19c4SJed Brown Logically Collective on TS 1386a43b19c4SJed Brown 1387a43b19c4SJed Brown Input Parameter: 1388a43b19c4SJed Brown + ts - the time-step context 138949354f04SShri Abhyankar - eftopt - exact final time option 1390a43b19c4SJed Brown 1391a43b19c4SJed Brown Level: beginner 1392a43b19c4SJed Brown 1393*a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1394a43b19c4SJed Brown @*/ 139549354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1396a43b19c4SJed Brown { 1397a43b19c4SJed Brown PetscFunctionBegin; 1398a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 139949354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 140049354f04SShri Abhyankar ts->exact_final_time = eftopt; 1401a43b19c4SJed Brown PetscFunctionReturn(0); 1402a43b19c4SJed Brown } 1403a43b19c4SJed Brown 1404a43b19c4SJed Brown #undef __FUNCT__ 14054a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1406d763cef2SBarry Smith /*@ 1407d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1408d763cef2SBarry Smith 1409d763cef2SBarry Smith Not Collective 1410d763cef2SBarry Smith 1411d763cef2SBarry Smith Input Parameter: 1412d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1413d763cef2SBarry Smith 1414d763cef2SBarry Smith Output Parameter: 1415d763cef2SBarry Smith . dt - the current timestep size 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith Level: intermediate 1418d763cef2SBarry Smith 1419d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1420d763cef2SBarry Smith 1421d763cef2SBarry Smith .keywords: TS, get, timestep 1422d763cef2SBarry Smith @*/ 14237087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1424d763cef2SBarry Smith { 1425d763cef2SBarry Smith PetscFunctionBegin; 14260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1427f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1428d763cef2SBarry Smith *dt = ts->time_step; 1429d763cef2SBarry Smith PetscFunctionReturn(0); 1430d763cef2SBarry Smith } 1431d763cef2SBarry Smith 14324a2ae208SSatish Balay #undef __FUNCT__ 14334a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1434d8e5e3e6SSatish Balay /*@ 1435d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1436d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1437d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1438d763cef2SBarry Smith the solution at the next timestep has been calculated. 1439d763cef2SBarry Smith 1440d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1441d763cef2SBarry Smith 1442d763cef2SBarry Smith Input Parameter: 1443d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1444d763cef2SBarry Smith 1445d763cef2SBarry Smith Output Parameter: 1446d763cef2SBarry Smith . v - the vector containing the solution 1447d763cef2SBarry Smith 1448d763cef2SBarry Smith Level: intermediate 1449d763cef2SBarry Smith 1450d763cef2SBarry Smith .seealso: TSGetTimeStep() 1451d763cef2SBarry Smith 1452d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1453d763cef2SBarry Smith @*/ 14547087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1455d763cef2SBarry Smith { 1456d763cef2SBarry Smith PetscFunctionBegin; 14570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14584482741eSBarry Smith PetscValidPointer(v,2); 14598737fe31SLisandro Dalcin *v = ts->vec_sol; 1460d763cef2SBarry Smith PetscFunctionReturn(0); 1461d763cef2SBarry Smith } 1462d763cef2SBarry Smith 1463bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 14644a2ae208SSatish Balay #undef __FUNCT__ 1465bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1466d8e5e3e6SSatish Balay /*@ 1467bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1468d763cef2SBarry Smith 1469bdad233fSMatthew Knepley Not collective 1470d763cef2SBarry Smith 1471bdad233fSMatthew Knepley Input Parameters: 1472bdad233fSMatthew Knepley + ts - The TS 1473bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1474d763cef2SBarry Smith .vb 14750910c330SBarry Smith U_t - A U = 0 (linear) 14760910c330SBarry Smith U_t - A(t) U = 0 (linear) 14770910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1478d763cef2SBarry Smith .ve 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith Level: beginner 1481d763cef2SBarry Smith 1482bdad233fSMatthew Knepley .keywords: TS, problem type 1483bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1484d763cef2SBarry Smith @*/ 14857087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1486a7cc72afSBarry Smith { 14879e2a6581SJed Brown PetscErrorCode ierr; 14889e2a6581SJed Brown 1489d763cef2SBarry Smith PetscFunctionBegin; 14900700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1491bdad233fSMatthew Knepley ts->problem_type = type; 14929e2a6581SJed Brown if (type == TS_LINEAR) { 14939e2a6581SJed Brown SNES snes; 14949e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 14959e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 14969e2a6581SJed Brown } 1497d763cef2SBarry Smith PetscFunctionReturn(0); 1498d763cef2SBarry Smith } 1499d763cef2SBarry Smith 1500bdad233fSMatthew Knepley #undef __FUNCT__ 1501bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1502bdad233fSMatthew Knepley /*@C 1503bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1504bdad233fSMatthew Knepley 1505bdad233fSMatthew Knepley Not collective 1506bdad233fSMatthew Knepley 1507bdad233fSMatthew Knepley Input Parameter: 1508bdad233fSMatthew Knepley . ts - The TS 1509bdad233fSMatthew Knepley 1510bdad233fSMatthew Knepley Output Parameter: 1511bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1512bdad233fSMatthew Knepley .vb 1513089b2837SJed Brown M U_t = A U 1514089b2837SJed Brown M(t) U_t = A(t) U 1515b5abc632SBarry Smith F(t,U,U_t) 1516bdad233fSMatthew Knepley .ve 1517bdad233fSMatthew Knepley 1518bdad233fSMatthew Knepley Level: beginner 1519bdad233fSMatthew Knepley 1520bdad233fSMatthew Knepley .keywords: TS, problem type 1521bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1522bdad233fSMatthew Knepley @*/ 15237087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1524a7cc72afSBarry Smith { 1525bdad233fSMatthew Knepley PetscFunctionBegin; 15260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 15274482741eSBarry Smith PetscValidIntPointer(type,2); 1528bdad233fSMatthew Knepley *type = ts->problem_type; 1529bdad233fSMatthew Knepley PetscFunctionReturn(0); 1530bdad233fSMatthew Knepley } 1531d763cef2SBarry Smith 15324a2ae208SSatish Balay #undef __FUNCT__ 15334a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1534d763cef2SBarry Smith /*@ 1535d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1536d763cef2SBarry Smith of a timestepper. 1537d763cef2SBarry Smith 1538d763cef2SBarry Smith Collective on TS 1539d763cef2SBarry Smith 1540d763cef2SBarry Smith Input Parameter: 1541d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1542d763cef2SBarry Smith 1543d763cef2SBarry Smith Notes: 1544d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1545d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1546d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1547d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1548d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1549d763cef2SBarry Smith 1550d763cef2SBarry Smith Level: advanced 1551d763cef2SBarry Smith 1552d763cef2SBarry Smith .keywords: TS, timestep, setup 1553d763cef2SBarry Smith 1554d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1555d763cef2SBarry Smith @*/ 15567087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1557d763cef2SBarry Smith { 1558dfbe8321SBarry Smith PetscErrorCode ierr; 15596c6b9e74SPeter Brune DM dm; 15606c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 15616c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 15626c6b9e74SPeter Brune TSIJacobian ijac; 15636c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1564d763cef2SBarry Smith 1565d763cef2SBarry Smith PetscFunctionBegin; 15660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1567277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1568277b19d0SLisandro Dalcin 15697adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 15709596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1571d763cef2SBarry Smith } 1572277b19d0SLisandro Dalcin 1573277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1574277b19d0SLisandro Dalcin 1575ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr); 15761c3436cfSJed Brown 1577277b19d0SLisandro Dalcin if (ts->ops->setup) { 1578000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1579277b19d0SLisandro Dalcin } 1580277b19d0SLisandro Dalcin 15816c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 15826c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 15836c6b9e74SPeter Brune */ 15846c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 15856c6b9e74SPeter Brune ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 15866c6b9e74SPeter Brune if (!func) { 15876c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 15886c6b9e74SPeter Brune } 15896c6b9e74SPeter Brune /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 15906c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 15916c6b9e74SPeter Brune */ 15926c6b9e74SPeter Brune ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 15936c6b9e74SPeter Brune ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 15946c6b9e74SPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 15956c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 15966c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 15976c6b9e74SPeter Brune } 1598277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1599277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1600277b19d0SLisandro Dalcin } 1601277b19d0SLisandro Dalcin 1602277b19d0SLisandro Dalcin #undef __FUNCT__ 1603277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1604277b19d0SLisandro Dalcin /*@ 1605277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1606277b19d0SLisandro Dalcin 1607277b19d0SLisandro Dalcin Collective on TS 1608277b19d0SLisandro Dalcin 1609277b19d0SLisandro Dalcin Input Parameter: 1610277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1611277b19d0SLisandro Dalcin 1612277b19d0SLisandro Dalcin Level: beginner 1613277b19d0SLisandro Dalcin 1614277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1615277b19d0SLisandro Dalcin 1616277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1617277b19d0SLisandro Dalcin @*/ 1618277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1619277b19d0SLisandro Dalcin { 1620277b19d0SLisandro Dalcin PetscErrorCode ierr; 1621277b19d0SLisandro Dalcin 1622277b19d0SLisandro Dalcin PetscFunctionBegin; 1623277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1624277b19d0SLisandro Dalcin if (ts->ops->reset) { 1625277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1626277b19d0SLisandro Dalcin } 1627277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 16284e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 16294e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1630214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 16316bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1632e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1633e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 163438637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1635277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1636d763cef2SBarry Smith PetscFunctionReturn(0); 1637d763cef2SBarry Smith } 1638d763cef2SBarry Smith 16394a2ae208SSatish Balay #undef __FUNCT__ 16404a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1641d8e5e3e6SSatish Balay /*@ 1642d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1643d763cef2SBarry Smith with TSCreate(). 1644d763cef2SBarry Smith 1645d763cef2SBarry Smith Collective on TS 1646d763cef2SBarry Smith 1647d763cef2SBarry Smith Input Parameter: 1648d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith Level: beginner 1651d763cef2SBarry Smith 1652d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1653d763cef2SBarry Smith 1654d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1655d763cef2SBarry Smith @*/ 16566bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1657d763cef2SBarry Smith { 16586849ba73SBarry Smith PetscErrorCode ierr; 1659d763cef2SBarry Smith 1660d763cef2SBarry Smith PetscFunctionBegin; 16616bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 16626bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 16636bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1664d763cef2SBarry Smith 16656bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1666277b19d0SLisandro Dalcin 1667be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 16686bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 16696bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 16706d4c513bSLisandro Dalcin 167184df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 16726bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 16736bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 16746bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 16756d4c513bSLisandro Dalcin 1676a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1677d763cef2SBarry Smith PetscFunctionReturn(0); 1678d763cef2SBarry Smith } 1679d763cef2SBarry Smith 16804a2ae208SSatish Balay #undef __FUNCT__ 16814a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1682d8e5e3e6SSatish Balay /*@ 1683d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1684d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1685d763cef2SBarry Smith 1686d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1687d763cef2SBarry Smith 1688d763cef2SBarry Smith Input Parameter: 1689d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1690d763cef2SBarry Smith 1691d763cef2SBarry Smith Output Parameter: 1692d763cef2SBarry Smith . snes - the nonlinear solver context 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith Notes: 1695d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1696d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 169794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1698d763cef2SBarry Smith 1699d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1700d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1701d763cef2SBarry Smith 1702d763cef2SBarry Smith Level: beginner 1703d763cef2SBarry Smith 1704d763cef2SBarry Smith .keywords: timestep, get, SNES 1705d763cef2SBarry Smith @*/ 17067087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1707d763cef2SBarry Smith { 1708d372ba47SLisandro Dalcin PetscErrorCode ierr; 1709d372ba47SLisandro Dalcin 1710d763cef2SBarry Smith PetscFunctionBegin; 17110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17124482741eSBarry Smith PetscValidPointer(snes,2); 1713d372ba47SLisandro Dalcin if (!ts->snes) { 1714d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 17156c6b9e74SPeter Brune ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1716d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1717d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1718496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 17199e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 17209e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 17219e2a6581SJed Brown } 1722d372ba47SLisandro Dalcin } 1723d763cef2SBarry Smith *snes = ts->snes; 1724d763cef2SBarry Smith PetscFunctionReturn(0); 1725d763cef2SBarry Smith } 1726d763cef2SBarry Smith 17274a2ae208SSatish Balay #undef __FUNCT__ 172894b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1729d8e5e3e6SSatish Balay /*@ 173094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1731d763cef2SBarry Smith a TS (timestepper) context. 1732d763cef2SBarry Smith 173394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1734d763cef2SBarry Smith 1735d763cef2SBarry Smith Input Parameter: 1736d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1737d763cef2SBarry Smith 1738d763cef2SBarry Smith Output Parameter: 173994b7f48cSBarry Smith . ksp - the nonlinear solver context 1740d763cef2SBarry Smith 1741d763cef2SBarry Smith Notes: 174294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1743d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1744d763cef2SBarry Smith KSP and PC contexts as well. 1745d763cef2SBarry Smith 174694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 174794b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1748d763cef2SBarry Smith 1749d763cef2SBarry Smith Level: beginner 1750d763cef2SBarry Smith 175194b7f48cSBarry Smith .keywords: timestep, get, KSP 1752d763cef2SBarry Smith @*/ 17537087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1754d763cef2SBarry Smith { 1755d372ba47SLisandro Dalcin PetscErrorCode ierr; 1756089b2837SJed Brown SNES snes; 1757d372ba47SLisandro Dalcin 1758d763cef2SBarry Smith PetscFunctionBegin; 17590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17604482741eSBarry Smith PetscValidPointer(ksp,2); 176117186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1762e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1763089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1764089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1765d763cef2SBarry Smith PetscFunctionReturn(0); 1766d763cef2SBarry Smith } 1767d763cef2SBarry Smith 1768d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1769d763cef2SBarry Smith 17704a2ae208SSatish Balay #undef __FUNCT__ 1771adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1772adb62b0dSMatthew Knepley /*@ 1773adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1774adb62b0dSMatthew Knepley maximum time for iteration. 1775adb62b0dSMatthew Knepley 17763f9fe445SBarry Smith Not Collective 1777adb62b0dSMatthew Knepley 1778adb62b0dSMatthew Knepley Input Parameters: 1779adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1780adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1781adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1782adb62b0dSMatthew Knepley 1783adb62b0dSMatthew Knepley Level: intermediate 1784adb62b0dSMatthew Knepley 1785adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1786adb62b0dSMatthew Knepley @*/ 17877087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1788adb62b0dSMatthew Knepley { 1789adb62b0dSMatthew Knepley PetscFunctionBegin; 17900700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1791abc0a331SBarry Smith if (maxsteps) { 17924482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1793adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1794adb62b0dSMatthew Knepley } 1795abc0a331SBarry Smith if (maxtime) { 17964482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1797adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1798adb62b0dSMatthew Knepley } 1799adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1800adb62b0dSMatthew Knepley } 1801adb62b0dSMatthew Knepley 1802adb62b0dSMatthew Knepley #undef __FUNCT__ 18034a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1804d763cef2SBarry Smith /*@ 1805d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1806d763cef2SBarry Smith maximum time for iteration. 1807d763cef2SBarry Smith 18083f9fe445SBarry Smith Logically Collective on TS 1809d763cef2SBarry Smith 1810d763cef2SBarry Smith Input Parameters: 1811d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1812d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1813d763cef2SBarry Smith - maxtime - final time to iterate to 1814d763cef2SBarry Smith 1815d763cef2SBarry Smith Options Database Keys: 1816d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 18173bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1818d763cef2SBarry Smith 1819d763cef2SBarry Smith Notes: 1820d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1821d763cef2SBarry Smith 1822d763cef2SBarry Smith Level: intermediate 1823d763cef2SBarry Smith 1824d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1825a43b19c4SJed Brown 1826a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1827d763cef2SBarry Smith @*/ 18287087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1829d763cef2SBarry Smith { 1830d763cef2SBarry Smith PetscFunctionBegin; 18310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1832c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1833c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 183439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 183539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1836d763cef2SBarry Smith PetscFunctionReturn(0); 1837d763cef2SBarry Smith } 1838d763cef2SBarry Smith 18394a2ae208SSatish Balay #undef __FUNCT__ 18404a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1841d763cef2SBarry Smith /*@ 1842d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1843d763cef2SBarry Smith for use by the TS routines. 1844d763cef2SBarry Smith 18453f9fe445SBarry Smith Logically Collective on TS and Vec 1846d763cef2SBarry Smith 1847d763cef2SBarry Smith Input Parameters: 1848d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 18490910c330SBarry Smith - u - the solution vector 1850d763cef2SBarry Smith 1851d763cef2SBarry Smith Level: beginner 1852d763cef2SBarry Smith 1853d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1854d763cef2SBarry Smith @*/ 18550910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 1856d763cef2SBarry Smith { 18578737fe31SLisandro Dalcin PetscErrorCode ierr; 1858496e6a7aSJed Brown DM dm; 18598737fe31SLisandro Dalcin 1860d763cef2SBarry Smith PetscFunctionBegin; 18610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18620910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 18630910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 18646bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 18650910c330SBarry Smith ts->vec_sol = u; 1866496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18670910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 1868d763cef2SBarry Smith PetscFunctionReturn(0); 1869d763cef2SBarry Smith } 1870d763cef2SBarry Smith 1871e74ef692SMatthew Knepley #undef __FUNCT__ 1872e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1873ac226902SBarry Smith /*@C 1874000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 18753f2090d5SJed Brown called once at the beginning of each time step. 1876000e7ae3SMatthew Knepley 18773f9fe445SBarry Smith Logically Collective on TS 1878000e7ae3SMatthew Knepley 1879000e7ae3SMatthew Knepley Input Parameters: 1880000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1881000e7ae3SMatthew Knepley - func - The function 1882000e7ae3SMatthew Knepley 1883000e7ae3SMatthew Knepley Calling sequence of func: 1884000e7ae3SMatthew Knepley . func (TS ts); 1885000e7ae3SMatthew Knepley 1886000e7ae3SMatthew Knepley Level: intermediate 1887000e7ae3SMatthew Knepley 1888b8123daeSJed Brown Note: 1889b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 1890b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 1891b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 1892b8123daeSJed Brown 1893000e7ae3SMatthew Knepley .keywords: TS, timestep 1894b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1895000e7ae3SMatthew Knepley @*/ 18967087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1897000e7ae3SMatthew Knepley { 1898000e7ae3SMatthew Knepley PetscFunctionBegin; 18990700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1900000e7ae3SMatthew Knepley ts->ops->prestep = func; 1901000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1902000e7ae3SMatthew Knepley } 1903000e7ae3SMatthew Knepley 1904e74ef692SMatthew Knepley #undef __FUNCT__ 19053f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 190609ee8438SJed Brown /*@ 19073f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 19083f2090d5SJed Brown 19093f2090d5SJed Brown Collective on TS 19103f2090d5SJed Brown 19113f2090d5SJed Brown Input Parameters: 19123f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 19133f2090d5SJed Brown 19143f2090d5SJed Brown Notes: 19153f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 19163f2090d5SJed Brown so most users would not generally call this routine themselves. 19173f2090d5SJed Brown 19183f2090d5SJed Brown Level: developer 19193f2090d5SJed Brown 19203f2090d5SJed Brown .keywords: TS, timestep 1921b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 19223f2090d5SJed Brown @*/ 19237087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 19243f2090d5SJed Brown { 19253f2090d5SJed Brown PetscErrorCode ierr; 19263f2090d5SJed Brown 19273f2090d5SJed Brown PetscFunctionBegin; 19280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 192972ac3e02SJed Brown if (ts->ops->prestep) { 19303f2090d5SJed Brown PetscStackPush("TS PreStep function"); 19313f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 19323f2090d5SJed Brown PetscStackPop; 1933312ce896SJed Brown } 19343f2090d5SJed Brown PetscFunctionReturn(0); 19353f2090d5SJed Brown } 19363f2090d5SJed Brown 19373f2090d5SJed Brown #undef __FUNCT__ 1938b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 1939b8123daeSJed Brown /*@C 1940b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 1941b8123daeSJed Brown called once at the beginning of each stage. 1942b8123daeSJed Brown 1943b8123daeSJed Brown Logically Collective on TS 1944b8123daeSJed Brown 1945b8123daeSJed Brown Input Parameters: 1946b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 1947b8123daeSJed Brown - func - The function 1948b8123daeSJed Brown 1949b8123daeSJed Brown Calling sequence of func: 1950b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 1951b8123daeSJed Brown 1952b8123daeSJed Brown Level: intermediate 1953b8123daeSJed Brown 1954b8123daeSJed Brown Note: 1955b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1956b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1957b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1958b8123daeSJed Brown 1959b8123daeSJed Brown .keywords: TS, timestep 1960b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1961b8123daeSJed Brown @*/ 1962b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1963b8123daeSJed Brown { 1964b8123daeSJed Brown PetscFunctionBegin; 1965b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1966b8123daeSJed Brown ts->ops->prestage = func; 1967b8123daeSJed Brown PetscFunctionReturn(0); 1968b8123daeSJed Brown } 1969b8123daeSJed Brown 1970b8123daeSJed Brown #undef __FUNCT__ 1971b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 1972b8123daeSJed Brown /*@ 1973b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1974b8123daeSJed Brown 1975b8123daeSJed Brown Collective on TS 1976b8123daeSJed Brown 1977b8123daeSJed Brown Input Parameters: 1978b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 1979b8123daeSJed Brown 1980b8123daeSJed Brown Notes: 1981b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 1982b8123daeSJed Brown most users would not generally call this routine themselves. 1983b8123daeSJed Brown 1984b8123daeSJed Brown Level: developer 1985b8123daeSJed Brown 1986b8123daeSJed Brown .keywords: TS, timestep 1987b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1988b8123daeSJed Brown @*/ 1989b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1990b8123daeSJed Brown { 1991b8123daeSJed Brown PetscErrorCode ierr; 1992b8123daeSJed Brown 1993b8123daeSJed Brown PetscFunctionBegin; 1994b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1995b8123daeSJed Brown if (ts->ops->prestage) { 1996b8123daeSJed Brown PetscStackPush("TS PreStage function"); 1997b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1998b8123daeSJed Brown PetscStackPop; 1999b8123daeSJed Brown } 2000b8123daeSJed Brown PetscFunctionReturn(0); 2001b8123daeSJed Brown } 2002b8123daeSJed Brown 2003b8123daeSJed Brown #undef __FUNCT__ 2004e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2005ac226902SBarry Smith /*@C 2006000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 20073f2090d5SJed Brown called once at the end of each time step. 2008000e7ae3SMatthew Knepley 20093f9fe445SBarry Smith Logically Collective on TS 2010000e7ae3SMatthew Knepley 2011000e7ae3SMatthew Knepley Input Parameters: 2012000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2013000e7ae3SMatthew Knepley - func - The function 2014000e7ae3SMatthew Knepley 2015000e7ae3SMatthew Knepley Calling sequence of func: 2016b8123daeSJed Brown $ func (TS ts); 2017000e7ae3SMatthew Knepley 2018000e7ae3SMatthew Knepley Level: intermediate 2019000e7ae3SMatthew Knepley 2020000e7ae3SMatthew Knepley .keywords: TS, timestep 2021b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2022000e7ae3SMatthew Knepley @*/ 20237087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2024000e7ae3SMatthew Knepley { 2025000e7ae3SMatthew Knepley PetscFunctionBegin; 20260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2027000e7ae3SMatthew Knepley ts->ops->poststep = func; 2028000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2029000e7ae3SMatthew Knepley } 2030000e7ae3SMatthew Knepley 2031e74ef692SMatthew Knepley #undef __FUNCT__ 20323f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 203309ee8438SJed Brown /*@ 20343f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 20353f2090d5SJed Brown 20363f2090d5SJed Brown Collective on TS 20373f2090d5SJed Brown 20383f2090d5SJed Brown Input Parameters: 20393f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 20403f2090d5SJed Brown 20413f2090d5SJed Brown Notes: 20423f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 20433f2090d5SJed Brown so most users would not generally call this routine themselves. 20443f2090d5SJed Brown 20453f2090d5SJed Brown Level: developer 20463f2090d5SJed Brown 20473f2090d5SJed Brown .keywords: TS, timestep 20483f2090d5SJed Brown @*/ 20497087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 20503f2090d5SJed Brown { 20513f2090d5SJed Brown PetscErrorCode ierr; 20523f2090d5SJed Brown 20533f2090d5SJed Brown PetscFunctionBegin; 20540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 205572ac3e02SJed Brown if (ts->ops->poststep) { 20563f2090d5SJed Brown PetscStackPush("TS PostStep function"); 20573f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 20583f2090d5SJed Brown PetscStackPop; 205972ac3e02SJed Brown } 20603f2090d5SJed Brown PetscFunctionReturn(0); 20613f2090d5SJed Brown } 20623f2090d5SJed Brown 2063d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2064d763cef2SBarry Smith 20654a2ae208SSatish Balay #undef __FUNCT__ 2066a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2067d763cef2SBarry Smith /*@C 2068a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2069d763cef2SBarry Smith timestep to display the iteration's progress. 2070d763cef2SBarry Smith 20713f9fe445SBarry Smith Logically Collective on TS 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith Input Parameters: 2074d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2075e213d8f1SJed Brown . monitor - monitoring routine 2076329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 2077b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 2078b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2079b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 2080d763cef2SBarry Smith 2081e213d8f1SJed Brown Calling sequence of monitor: 20820910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith + ts - the TS context 208588c05cc5SBarry Smith . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 208688c05cc5SBarry Smith been interpolated to) 20871f06c33eSBarry Smith . time - current time 20880910c330SBarry Smith . u - current iterate 2089d763cef2SBarry Smith - mctx - [optional] monitoring context 2090d763cef2SBarry Smith 2091d763cef2SBarry Smith Notes: 2092d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2093d763cef2SBarry Smith already has been loaded. 2094d763cef2SBarry Smith 2095025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2096025f1a04SBarry Smith 2097d763cef2SBarry Smith Level: intermediate 2098d763cef2SBarry Smith 2099d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2100d763cef2SBarry Smith 2101a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2102d763cef2SBarry Smith @*/ 2103c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2104d763cef2SBarry Smith { 2105d763cef2SBarry Smith PetscFunctionBegin; 21060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 210717186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2108d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 21098704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2110d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2111d763cef2SBarry Smith PetscFunctionReturn(0); 2112d763cef2SBarry Smith } 2113d763cef2SBarry Smith 21144a2ae208SSatish Balay #undef __FUNCT__ 2115a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2116d763cef2SBarry Smith /*@C 2117a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2118d763cef2SBarry Smith 21193f9fe445SBarry Smith Logically Collective on TS 2120d763cef2SBarry Smith 2121d763cef2SBarry Smith Input Parameters: 2122d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2123d763cef2SBarry Smith 2124d763cef2SBarry Smith Notes: 2125d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2126d763cef2SBarry Smith 2127d763cef2SBarry Smith Level: intermediate 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2130d763cef2SBarry Smith 2131a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2132d763cef2SBarry Smith @*/ 21337087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2134d763cef2SBarry Smith { 2135d952e501SBarry Smith PetscErrorCode ierr; 2136d952e501SBarry Smith PetscInt i; 2137d952e501SBarry Smith 2138d763cef2SBarry Smith PetscFunctionBegin; 21390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2140d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 21418704b422SBarry Smith if (ts->monitordestroy[i]) { 21428704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2143d952e501SBarry Smith } 2144d952e501SBarry Smith } 2145d763cef2SBarry Smith ts->numbermonitors = 0; 2146d763cef2SBarry Smith PetscFunctionReturn(0); 2147d763cef2SBarry Smith } 2148d763cef2SBarry Smith 21494a2ae208SSatish Balay #undef __FUNCT__ 2150a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2151d8e5e3e6SSatish Balay /*@ 2152a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 21535516499fSSatish Balay 21545516499fSSatish Balay Level: intermediate 215541251cbbSSatish Balay 21565516499fSSatish Balay .keywords: TS, set, monitor 21575516499fSSatish Balay 215841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 215941251cbbSSatish Balay @*/ 2160649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2161d763cef2SBarry Smith { 2162dfbe8321SBarry Smith PetscErrorCode ierr; 2163649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2164d132466eSBarry Smith 2165d763cef2SBarry Smith PetscFunctionBegin; 2166649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2167971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2168649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2169d763cef2SBarry Smith PetscFunctionReturn(0); 2170d763cef2SBarry Smith } 2171d763cef2SBarry Smith 21724a2ae208SSatish Balay #undef __FUNCT__ 2173cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2174cd652676SJed Brown /*@ 2175cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2176cd652676SJed Brown 2177cd652676SJed Brown Logically Collective on TS 2178cd652676SJed Brown 2179cd652676SJed Brown Input Argument: 2180cd652676SJed Brown . ts - time stepping context 2181cd652676SJed Brown 2182cd652676SJed Brown Output Argument: 2183cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2184cd652676SJed Brown 2185cd652676SJed Brown Level: intermediate 2186cd652676SJed Brown 2187cd652676SJed Brown .keywords: TS, set 2188cd652676SJed Brown 2189cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2190cd652676SJed Brown @*/ 2191cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2192cd652676SJed Brown { 2193cd652676SJed Brown PetscFunctionBegin; 2194cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2195cd652676SJed Brown ts->retain_stages = flg; 2196cd652676SJed Brown PetscFunctionReturn(0); 2197cd652676SJed Brown } 2198cd652676SJed Brown 2199cd652676SJed Brown #undef __FUNCT__ 2200cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2201cd652676SJed Brown /*@ 2202cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2203cd652676SJed Brown 2204cd652676SJed Brown Collective on TS 2205cd652676SJed Brown 2206cd652676SJed Brown Input Argument: 2207cd652676SJed Brown + ts - time stepping context 2208cd652676SJed Brown - t - time to interpolate to 2209cd652676SJed Brown 2210cd652676SJed Brown Output Argument: 22110910c330SBarry Smith . U - state at given time 2212cd652676SJed Brown 2213cd652676SJed Brown Notes: 2214cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2215cd652676SJed Brown 2216cd652676SJed Brown Level: intermediate 2217cd652676SJed Brown 2218cd652676SJed Brown Developer Notes: 2219cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2220cd652676SJed Brown 2221cd652676SJed Brown .keywords: TS, set 2222cd652676SJed Brown 2223cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2224cd652676SJed Brown @*/ 22250910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2226cd652676SJed Brown { 2227cd652676SJed Brown PetscErrorCode ierr; 2228cd652676SJed Brown 2229cd652676SJed Brown PetscFunctionBegin; 2230cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2231d8cd7023SBarry 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); 2232cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 22330910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2234cd652676SJed Brown PetscFunctionReturn(0); 2235cd652676SJed Brown } 2236cd652676SJed Brown 2237cd652676SJed Brown #undef __FUNCT__ 22384a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2239d763cef2SBarry Smith /*@ 22406d9e5789SSean Farley TSStep - Steps one time step 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith Collective on TS 2243d763cef2SBarry Smith 2244d763cef2SBarry Smith Input Parameter: 2245d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2246d763cef2SBarry Smith 22476d9e5789SSean Farley Level: intermediate 2248d763cef2SBarry Smith 2249b8123daeSJed Brown Notes: 2250b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2251b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2252b8123daeSJed Brown 225325cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 225425cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 225525cb2221SBarry Smith 2256d763cef2SBarry Smith .keywords: TS, timestep, solve 2257d763cef2SBarry Smith 225825cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 2259d763cef2SBarry Smith @*/ 2260193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2261d763cef2SBarry Smith { 2262362cd11cSLisandro Dalcin PetscReal ptime_prev; 2263dfbe8321SBarry Smith PetscErrorCode ierr; 2264d763cef2SBarry Smith 2265d763cef2SBarry Smith PetscFunctionBegin; 22660700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2267d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2268d405a339SMatthew Knepley 2269362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2270362cd11cSLisandro Dalcin 2271362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2272d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2273193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2274d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2275362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2276362cd11cSLisandro Dalcin 2277362cd11cSLisandro Dalcin if (ts->reason < 0) { 2278cef5090cSJed Brown if (ts->errorifstepfailed) { 2279cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2280cef5090cSJed Brown SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 2281cef5090cSJed Brown } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2282cef5090cSJed Brown } 2283362cd11cSLisandro Dalcin } else if (!ts->reason) { 2284362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2285362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2286362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2287362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2288362cd11cSLisandro Dalcin } 2289362cd11cSLisandro Dalcin 2290d763cef2SBarry Smith PetscFunctionReturn(0); 2291d763cef2SBarry Smith } 2292d763cef2SBarry Smith 22934a2ae208SSatish Balay #undef __FUNCT__ 229405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 229505175c85SJed Brown /*@ 229605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 229705175c85SJed Brown 22981c3436cfSJed Brown Collective on TS 229905175c85SJed Brown 230005175c85SJed Brown Input Arguments: 23011c3436cfSJed Brown + ts - time stepping context 23021c3436cfSJed Brown . order - desired order of accuracy 23031c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 230405175c85SJed Brown 230505175c85SJed Brown Output Arguments: 23060910c330SBarry Smith . U - state at the end of the current step 230705175c85SJed Brown 230805175c85SJed Brown Level: advanced 230905175c85SJed Brown 2310108c343cSJed Brown Notes: 2311108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2312108c343cSJed 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. 2313108c343cSJed Brown 23141c3436cfSJed Brown .seealso: TSStep(), TSAdapt 231505175c85SJed Brown @*/ 23160910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 231705175c85SJed Brown { 231805175c85SJed Brown PetscErrorCode ierr; 231905175c85SJed Brown 232005175c85SJed Brown PetscFunctionBegin; 232105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 232205175c85SJed Brown PetscValidType(ts,1); 23230910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 23241c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 23250910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 232605175c85SJed Brown PetscFunctionReturn(0); 232705175c85SJed Brown } 232805175c85SJed Brown 232905175c85SJed Brown #undef __FUNCT__ 23306a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 23316a4d4014SLisandro Dalcin /*@ 23326a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 23336a4d4014SLisandro Dalcin 23346a4d4014SLisandro Dalcin Collective on TS 23356a4d4014SLisandro Dalcin 23366a4d4014SLisandro Dalcin Input Parameter: 23376a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2338cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 23395a3a76d0SJed Brown 23406a4d4014SLisandro Dalcin Level: beginner 23416a4d4014SLisandro Dalcin 23425a3a76d0SJed Brown Notes: 23435a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 23445a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 23455a3a76d0SJed Brown stepped over the final time. 23465a3a76d0SJed Brown 23476a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 23486a4d4014SLisandro Dalcin 23496a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 23506a4d4014SLisandro Dalcin @*/ 2351cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 23526a4d4014SLisandro Dalcin { 23534d7d938eSLisandro Dalcin PetscBool flg; 23544d7d938eSLisandro Dalcin PetscViewer viewer; 23556a4d4014SLisandro Dalcin PetscErrorCode ierr; 2356cffb1e40SBarry Smith PetscViewerFormat format; 2357f22f69f0SBarry Smith 23586a4d4014SLisandro Dalcin PetscFunctionBegin; 23590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2360f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 236149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 23620910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 23635a3a76d0SJed Brown Vec y; 23640910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 23655a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 23665a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 23675a3a76d0SJed Brown } 2368f2c2a1b9SBarry Smith if (u) { 23690910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2370f2c2a1b9SBarry Smith } 23715a3a76d0SJed Brown } else { 2372f2c2a1b9SBarry Smith if (u) { 23730910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 23745a3a76d0SJed Brown } 2375f2c2a1b9SBarry Smith } 2376b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 23776a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2378193ac0bcSJed Brown ts->steps = 0; 23795ef26d82SJed Brown ts->ksp_its = 0; 23805ef26d82SJed Brown ts->snes_its = 0; 2381c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2382c610991cSLisandro Dalcin ts->reject = 0; 2383193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2384193ac0bcSJed Brown 2385193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2386193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 23870910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2388cc708dedSBarry Smith ts->solvetime = ts->ptime; 2389193ac0bcSJed Brown } else { 23906a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2391362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2392362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2393362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2394362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2395362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2396e1a7a14fSJed Brown while (!ts->reason) { 2397193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2398193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2399193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2400193ac0bcSJed Brown } 240149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 24020910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2403cc708dedSBarry Smith ts->solvetime = ts->max_time; 24040574a7fbSJed Brown } else { 2405ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2406cc708dedSBarry Smith ts->solvetime = ts->ptime; 24070574a7fbSJed Brown } 2408193ac0bcSJed Brown } 24093923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2410cffb1e40SBarry Smith ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr); 24114d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 2412cffb1e40SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 24134d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 2414cffb1e40SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 2415cffb1e40SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 24162b0a91c0SBarry Smith } 24176a4d4014SLisandro Dalcin PetscFunctionReturn(0); 24186a4d4014SLisandro Dalcin } 24196a4d4014SLisandro Dalcin 24206a4d4014SLisandro Dalcin #undef __FUNCT__ 24214a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2422228d79bcSJed Brown /*@ 2423228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2424228d79bcSJed Brown 2425228d79bcSJed Brown Collective on TS 2426228d79bcSJed Brown 2427228d79bcSJed Brown Input Parameters: 2428228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2429228d79bcSJed Brown . step - step number that has just completed 2430228d79bcSJed Brown . ptime - model time of the state 24310910c330SBarry Smith - u - state at the current model time 2432228d79bcSJed Brown 2433228d79bcSJed Brown Notes: 2434228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2435228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2436228d79bcSJed Brown 2437228d79bcSJed Brown Level: advanced 2438228d79bcSJed Brown 2439228d79bcSJed Brown .keywords: TS, timestep 2440228d79bcSJed Brown @*/ 24410910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2442d763cef2SBarry Smith { 24436849ba73SBarry Smith PetscErrorCode ierr; 2444a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2445d763cef2SBarry Smith 2446d763cef2SBarry Smith PetscFunctionBegin; 2447d763cef2SBarry Smith for (i=0; i<n; i++) { 24480910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2449d763cef2SBarry Smith } 2450d763cef2SBarry Smith PetscFunctionReturn(0); 2451d763cef2SBarry Smith } 2452d763cef2SBarry Smith 2453d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 24540b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 24550b039ecaSBarry Smith PetscDrawLG lg; 24560b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2457201da799SBarry Smith PetscInt ksp_its,snes_its; 24580b039ecaSBarry Smith }; 24590b039ecaSBarry Smith 2460d763cef2SBarry Smith 24614a2ae208SSatish Balay #undef __FUNCT__ 2462a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2463d763cef2SBarry Smith /*@C 2464a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2465a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2466d763cef2SBarry Smith 2467d763cef2SBarry Smith Collective on TS 2468d763cef2SBarry Smith 2469d763cef2SBarry Smith Input Parameters: 2470d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2471d763cef2SBarry Smith . label - the title to put in the title bar 24727c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2473a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2474a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2475d763cef2SBarry Smith 2476d763cef2SBarry Smith Output Parameter: 24770b039ecaSBarry Smith . ctx - the context 2478d763cef2SBarry Smith 2479d763cef2SBarry Smith Options Database Key: 24804f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 24814f09c107SBarry Smith . -ts_monitor_lg_solution - 248299fdda47SBarry Smith . -ts_monitor_lg_error - 248399fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 248499fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 248599fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2486d763cef2SBarry Smith 2487d763cef2SBarry Smith Notes: 2488a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2489d763cef2SBarry Smith 2490d763cef2SBarry Smith Level: intermediate 2491d763cef2SBarry Smith 24927c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2493d763cef2SBarry Smith 24944f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 24957c922b88SBarry Smith 2496d763cef2SBarry Smith @*/ 2497a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2498d763cef2SBarry Smith { 2499b0a32e0cSBarry Smith PetscDraw win; 2500dfbe8321SBarry Smith PetscErrorCode ierr; 250199fdda47SBarry Smith PetscBool flg = PETSC_TRUE; 2502d763cef2SBarry Smith 2503d763cef2SBarry Smith PetscFunctionBegin; 2504201da799SBarry Smith ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2505a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 25063fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 25070b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 250899fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr); 250999fdda47SBarry Smith if (flg) { 25100b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 251199fdda47SBarry Smith } 25120b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2513a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2514d763cef2SBarry Smith PetscFunctionReturn(0); 2515d763cef2SBarry Smith } 2516d763cef2SBarry Smith 25174a2ae208SSatish Balay #undef __FUNCT__ 25184f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 25194f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2520d763cef2SBarry Smith { 25210b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2522c32365f1SBarry Smith PetscReal x = ptime,y; 2523dfbe8321SBarry Smith PetscErrorCode ierr; 2524d763cef2SBarry Smith 2525d763cef2SBarry Smith PetscFunctionBegin; 2526a9f9c1f6SBarry Smith if (!n) { 2527a9f9c1f6SBarry Smith PetscDrawAxis axis; 2528a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2529a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2530a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2531a9f9c1f6SBarry Smith } 2532c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 25330b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 253499fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 25350b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 25363923b477SBarry Smith } 2537d763cef2SBarry Smith PetscFunctionReturn(0); 2538d763cef2SBarry Smith } 2539d763cef2SBarry Smith 25404a2ae208SSatish Balay #undef __FUNCT__ 2541a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2542d763cef2SBarry Smith /*@C 2543a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2544a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2545d763cef2SBarry Smith 25460b039ecaSBarry Smith Collective on TSMonitorLGCtx 2547d763cef2SBarry Smith 2548d763cef2SBarry Smith Input Parameter: 25490b039ecaSBarry Smith . ctx - the monitor context 2550d763cef2SBarry Smith 2551d763cef2SBarry Smith Level: intermediate 2552d763cef2SBarry Smith 2553d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2554d763cef2SBarry Smith 25554f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2556d763cef2SBarry Smith @*/ 2557a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2558d763cef2SBarry Smith { 2559b0a32e0cSBarry Smith PetscDraw draw; 2560dfbe8321SBarry Smith PetscErrorCode ierr; 2561d763cef2SBarry Smith 2562d763cef2SBarry Smith PetscFunctionBegin; 25630b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 25646bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 25650b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 25660b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2567d763cef2SBarry Smith PetscFunctionReturn(0); 2568d763cef2SBarry Smith } 2569d763cef2SBarry Smith 25704a2ae208SSatish Balay #undef __FUNCT__ 25714a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2572d763cef2SBarry Smith /*@ 2573b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2574d763cef2SBarry Smith 2575d763cef2SBarry Smith Not Collective 2576d763cef2SBarry Smith 2577d763cef2SBarry Smith Input Parameter: 2578d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2579d763cef2SBarry Smith 2580d763cef2SBarry Smith Output Parameter: 2581d763cef2SBarry Smith . t - the current time 2582d763cef2SBarry Smith 2583d763cef2SBarry Smith Level: beginner 2584d763cef2SBarry Smith 2585b8123daeSJed Brown Note: 2586b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2587b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2588b8123daeSJed Brown 2589d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2590d763cef2SBarry Smith 2591d763cef2SBarry Smith .keywords: TS, get, time 2592d763cef2SBarry Smith @*/ 25937087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2594d763cef2SBarry Smith { 2595d763cef2SBarry Smith PetscFunctionBegin; 25960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2597f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2598d763cef2SBarry Smith *t = ts->ptime; 2599d763cef2SBarry Smith PetscFunctionReturn(0); 2600d763cef2SBarry Smith } 2601d763cef2SBarry Smith 26024a2ae208SSatish Balay #undef __FUNCT__ 26036a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 26046a4d4014SLisandro Dalcin /*@ 26056a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 26066a4d4014SLisandro Dalcin 26073f9fe445SBarry Smith Logically Collective on TS 26086a4d4014SLisandro Dalcin 26096a4d4014SLisandro Dalcin Input Parameters: 26106a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 26116a4d4014SLisandro Dalcin - time - the time 26126a4d4014SLisandro Dalcin 26136a4d4014SLisandro Dalcin Level: intermediate 26146a4d4014SLisandro Dalcin 26156a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 26166a4d4014SLisandro Dalcin 26176a4d4014SLisandro Dalcin .keywords: TS, set, time 26186a4d4014SLisandro Dalcin @*/ 26197087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 26206a4d4014SLisandro Dalcin { 26216a4d4014SLisandro Dalcin PetscFunctionBegin; 26220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2623c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 26246a4d4014SLisandro Dalcin ts->ptime = t; 26256a4d4014SLisandro Dalcin PetscFunctionReturn(0); 26266a4d4014SLisandro Dalcin } 26276a4d4014SLisandro Dalcin 26286a4d4014SLisandro Dalcin #undef __FUNCT__ 26294a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2630d763cef2SBarry Smith /*@C 2631d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2632d763cef2SBarry Smith TS options in the database. 2633d763cef2SBarry Smith 26343f9fe445SBarry Smith Logically Collective on TS 2635d763cef2SBarry Smith 2636d763cef2SBarry Smith Input Parameter: 2637d763cef2SBarry Smith + ts - The TS context 2638d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2639d763cef2SBarry Smith 2640d763cef2SBarry Smith Notes: 2641d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2642d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2643d763cef2SBarry Smith hyphen. 2644d763cef2SBarry Smith 2645d763cef2SBarry Smith Level: advanced 2646d763cef2SBarry Smith 2647d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2648d763cef2SBarry Smith 2649d763cef2SBarry Smith .seealso: TSSetFromOptions() 2650d763cef2SBarry Smith 2651d763cef2SBarry Smith @*/ 26527087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2653d763cef2SBarry Smith { 2654dfbe8321SBarry Smith PetscErrorCode ierr; 2655089b2837SJed Brown SNES snes; 2656d763cef2SBarry Smith 2657d763cef2SBarry Smith PetscFunctionBegin; 26580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2659d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2660089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2661089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2662d763cef2SBarry Smith PetscFunctionReturn(0); 2663d763cef2SBarry Smith } 2664d763cef2SBarry Smith 2665d763cef2SBarry Smith 26664a2ae208SSatish Balay #undef __FUNCT__ 26674a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2668d763cef2SBarry Smith /*@C 2669d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2670d763cef2SBarry Smith TS options in the database. 2671d763cef2SBarry Smith 26723f9fe445SBarry Smith Logically Collective on TS 2673d763cef2SBarry Smith 2674d763cef2SBarry Smith Input Parameter: 2675d763cef2SBarry Smith + ts - The TS context 2676d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2677d763cef2SBarry Smith 2678d763cef2SBarry Smith Notes: 2679d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2680d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2681d763cef2SBarry Smith hyphen. 2682d763cef2SBarry Smith 2683d763cef2SBarry Smith Level: advanced 2684d763cef2SBarry Smith 2685d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2686d763cef2SBarry Smith 2687d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2688d763cef2SBarry Smith 2689d763cef2SBarry Smith @*/ 26907087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2691d763cef2SBarry Smith { 2692dfbe8321SBarry Smith PetscErrorCode ierr; 2693089b2837SJed Brown SNES snes; 2694d763cef2SBarry Smith 2695d763cef2SBarry Smith PetscFunctionBegin; 26960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2697d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2698089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2699089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2700d763cef2SBarry Smith PetscFunctionReturn(0); 2701d763cef2SBarry Smith } 2702d763cef2SBarry Smith 27034a2ae208SSatish Balay #undef __FUNCT__ 27044a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2705d763cef2SBarry Smith /*@C 2706d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2707d763cef2SBarry Smith TS options in the database. 2708d763cef2SBarry Smith 2709d763cef2SBarry Smith Not Collective 2710d763cef2SBarry Smith 2711d763cef2SBarry Smith Input Parameter: 2712d763cef2SBarry Smith . ts - The TS context 2713d763cef2SBarry Smith 2714d763cef2SBarry Smith Output Parameter: 2715d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2716d763cef2SBarry Smith 2717d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2718d763cef2SBarry Smith sufficient length to hold the prefix. 2719d763cef2SBarry Smith 2720d763cef2SBarry Smith Level: intermediate 2721d763cef2SBarry Smith 2722d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2723d763cef2SBarry Smith 2724d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2725d763cef2SBarry Smith @*/ 27267087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2727d763cef2SBarry Smith { 2728dfbe8321SBarry Smith PetscErrorCode ierr; 2729d763cef2SBarry Smith 2730d763cef2SBarry Smith PetscFunctionBegin; 27310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27324482741eSBarry Smith PetscValidPointer(prefix,2); 2733d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2734d763cef2SBarry Smith PetscFunctionReturn(0); 2735d763cef2SBarry Smith } 2736d763cef2SBarry Smith 27374a2ae208SSatish Balay #undef __FUNCT__ 27384a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2739d763cef2SBarry Smith /*@C 2740d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2741d763cef2SBarry Smith 2742d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2743d763cef2SBarry Smith 2744d763cef2SBarry Smith Input Parameter: 2745d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2746d763cef2SBarry Smith 2747d763cef2SBarry Smith Output Parameters: 2748b5abc632SBarry Smith + J - The Jacobian J of F, where U_t = G(U,t) 2749d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2750089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2751d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2752d763cef2SBarry Smith 2753d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2754d763cef2SBarry Smith 2755d763cef2SBarry Smith Level: intermediate 2756d763cef2SBarry Smith 275726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2758d763cef2SBarry Smith 2759d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2760d763cef2SBarry Smith @*/ 2761089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2762d763cef2SBarry Smith { 2763089b2837SJed Brown PetscErrorCode ierr; 2764089b2837SJed Brown SNES snes; 276524989b8cSPeter Brune DM dm; 2766089b2837SJed Brown 2767d763cef2SBarry Smith PetscFunctionBegin; 2768089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2769089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 277024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 277124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2772d763cef2SBarry Smith PetscFunctionReturn(0); 2773d763cef2SBarry Smith } 2774d763cef2SBarry Smith 27751713a123SBarry Smith #undef __FUNCT__ 27762eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 27772eca1d9cSJed Brown /*@C 27782eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 27792eca1d9cSJed Brown 27802eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 27812eca1d9cSJed Brown 27822eca1d9cSJed Brown Input Parameter: 27832eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 27842eca1d9cSJed Brown 27852eca1d9cSJed Brown Output Parameters: 27862eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 27872eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 27882eca1d9cSJed Brown . f - The function to compute the matrices 27892eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 27902eca1d9cSJed Brown 27912eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 27922eca1d9cSJed Brown 27932eca1d9cSJed Brown Level: advanced 27942eca1d9cSJed Brown 27952eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 27962eca1d9cSJed Brown 27972eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 27982eca1d9cSJed Brown @*/ 27997087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 28002eca1d9cSJed Brown { 2801089b2837SJed Brown PetscErrorCode ierr; 2802089b2837SJed Brown SNES snes; 280324989b8cSPeter Brune DM dm; 28040910c330SBarry Smith 28052eca1d9cSJed Brown PetscFunctionBegin; 2806089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2807f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 2808089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 280924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 281024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 28112eca1d9cSJed Brown PetscFunctionReturn(0); 28122eca1d9cSJed Brown } 28132eca1d9cSJed Brown 281483a4ac43SBarry Smith struct _n_TSMonitorDrawCtx { 28156083293cSBarry Smith PetscViewer viewer; 28166083293cSBarry Smith Vec initialsolution; 28176083293cSBarry Smith PetscBool showinitial; 281883a4ac43SBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 281983a4ac43SBarry Smith }; 28206083293cSBarry Smith 28212eca1d9cSJed Brown #undef __FUNCT__ 282283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 28231713a123SBarry Smith /*@C 282483a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 28251713a123SBarry Smith VecView() for the solution at each timestep 28261713a123SBarry Smith 28271713a123SBarry Smith Collective on TS 28281713a123SBarry Smith 28291713a123SBarry Smith Input Parameters: 28301713a123SBarry Smith + ts - the TS context 28311713a123SBarry Smith . step - current time-step 2832142b95e3SSatish Balay . ptime - current time 28331713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 28341713a123SBarry Smith 283599fdda47SBarry Smith Options Database: 283699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 283799fdda47SBarry Smith 283899fdda47SBarry Smith Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 283999fdda47SBarry Smith will look bad 284099fdda47SBarry Smith 28411713a123SBarry Smith Level: intermediate 28421713a123SBarry Smith 28431713a123SBarry Smith .keywords: TS, vector, monitor, view 28441713a123SBarry Smith 2845a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 28461713a123SBarry Smith @*/ 28470910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 28481713a123SBarry Smith { 2849dfbe8321SBarry Smith PetscErrorCode ierr; 285083a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 28511713a123SBarry Smith 28521713a123SBarry Smith PetscFunctionBegin; 28536083293cSBarry Smith if (!step && ictx->showinitial) { 28546083293cSBarry Smith if (!ictx->initialsolution) { 28550910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 28561713a123SBarry Smith } 28570910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 28586083293cSBarry Smith } 28593fbbecb0SBarry Smith if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 28600dcf80beSBarry Smith 28616083293cSBarry Smith if (ictx->showinitial) { 28626083293cSBarry Smith PetscReal pause; 28636083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 28646083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 28656083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 28666083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 28676083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 28686083293cSBarry Smith } 28690910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 28706083293cSBarry Smith if (ictx->showinitial) { 28716083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 28726083293cSBarry Smith } 28731713a123SBarry Smith PetscFunctionReturn(0); 28741713a123SBarry Smith } 28751713a123SBarry Smith 28761713a123SBarry Smith 28776c699258SBarry Smith #undef __FUNCT__ 287883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 28796083293cSBarry Smith /*@C 288083a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 28816083293cSBarry Smith 28826083293cSBarry Smith Collective on TS 28836083293cSBarry Smith 28846083293cSBarry Smith Input Parameters: 28856083293cSBarry Smith . ctx - the monitor context 28866083293cSBarry Smith 28876083293cSBarry Smith Level: intermediate 28886083293cSBarry Smith 28896083293cSBarry Smith .keywords: TS, vector, monitor, view 28906083293cSBarry Smith 289183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 28926083293cSBarry Smith @*/ 289383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 28946083293cSBarry Smith { 28956083293cSBarry Smith PetscErrorCode ierr; 28966083293cSBarry Smith 28976083293cSBarry Smith PetscFunctionBegin; 289883a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 289983a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 290083a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 29016083293cSBarry Smith PetscFunctionReturn(0); 29026083293cSBarry Smith } 29036083293cSBarry Smith 29046083293cSBarry Smith #undef __FUNCT__ 290583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 29066083293cSBarry Smith /*@C 290783a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 29086083293cSBarry Smith 29096083293cSBarry Smith Collective on TS 29106083293cSBarry Smith 29116083293cSBarry Smith Input Parameter: 29126083293cSBarry Smith . ts - time-step context 29136083293cSBarry Smith 29146083293cSBarry Smith Output Patameter: 29156083293cSBarry Smith . ctx - the monitor context 29166083293cSBarry Smith 291799fdda47SBarry Smith Options Database: 291899fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 291999fdda47SBarry Smith 29206083293cSBarry Smith Level: intermediate 29216083293cSBarry Smith 29226083293cSBarry Smith .keywords: TS, vector, monitor, view 29236083293cSBarry Smith 292483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 29256083293cSBarry Smith @*/ 292683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 29276083293cSBarry Smith { 29286083293cSBarry Smith PetscErrorCode ierr; 29296083293cSBarry Smith 29306083293cSBarry Smith PetscFunctionBegin; 293183a4ac43SBarry Smith ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 293283a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 293383a4ac43SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 293483a4ac43SBarry Smith (*ctx)->howoften = howoften; 293599fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 29366083293cSBarry Smith PetscFunctionReturn(0); 29376083293cSBarry Smith } 29386083293cSBarry Smith 29396083293cSBarry Smith #undef __FUNCT__ 294083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 29413a471f94SBarry Smith /*@C 294283a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 29433a471f94SBarry Smith VecView() for the error at each timestep 29443a471f94SBarry Smith 29453a471f94SBarry Smith Collective on TS 29463a471f94SBarry Smith 29473a471f94SBarry Smith Input Parameters: 29483a471f94SBarry Smith + ts - the TS context 29493a471f94SBarry Smith . step - current time-step 29503a471f94SBarry Smith . ptime - current time 29513a471f94SBarry Smith - dummy - either a viewer or PETSC_NULL 29523a471f94SBarry Smith 29533a471f94SBarry Smith Level: intermediate 29543a471f94SBarry Smith 29553a471f94SBarry Smith .keywords: TS, vector, monitor, view 29563a471f94SBarry Smith 29573a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 29583a471f94SBarry Smith @*/ 29590910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 29603a471f94SBarry Smith { 29613a471f94SBarry Smith PetscErrorCode ierr; 296283a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 296383a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 29643a471f94SBarry Smith Vec work; 29653a471f94SBarry Smith 29663a471f94SBarry Smith PetscFunctionBegin; 29673fbbecb0SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 29680910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 29693a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 29700910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 29713a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 29723a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 29733a471f94SBarry Smith PetscFunctionReturn(0); 29743a471f94SBarry Smith } 29753a471f94SBarry Smith 29762a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 29773a471f94SBarry Smith #undef __FUNCT__ 29786c699258SBarry Smith #define __FUNCT__ "TSSetDM" 29796c699258SBarry Smith /*@ 29806c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 29816c699258SBarry Smith 29823f9fe445SBarry Smith Logically Collective on TS and DM 29836c699258SBarry Smith 29846c699258SBarry Smith Input Parameters: 29856c699258SBarry Smith + ts - the preconditioner context 29866c699258SBarry Smith - dm - the dm 29876c699258SBarry Smith 29886c699258SBarry Smith Level: intermediate 29896c699258SBarry Smith 29906c699258SBarry Smith 29916c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 29926c699258SBarry Smith @*/ 29937087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 29946c699258SBarry Smith { 29956c699258SBarry Smith PetscErrorCode ierr; 2996089b2837SJed Brown SNES snes; 2997942e3340SBarry Smith DMTS tsdm; 29986c699258SBarry Smith 29996c699258SBarry Smith PetscFunctionBegin; 30000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 300170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 3002942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 30032a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 3004942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 3005942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 300624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 300724989b8cSPeter Brune tsdm->originaldm = dm; 300824989b8cSPeter Brune } 300924989b8cSPeter Brune } 30106bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 301124989b8cSPeter Brune } 30126c699258SBarry Smith ts->dm = dm; 3013089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3014089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 30156c699258SBarry Smith PetscFunctionReturn(0); 30166c699258SBarry Smith } 30176c699258SBarry Smith 30186c699258SBarry Smith #undef __FUNCT__ 30196c699258SBarry Smith #define __FUNCT__ "TSGetDM" 30206c699258SBarry Smith /*@ 30216c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 30226c699258SBarry Smith 30233f9fe445SBarry Smith Not Collective 30246c699258SBarry Smith 30256c699258SBarry Smith Input Parameter: 30266c699258SBarry Smith . ts - the preconditioner context 30276c699258SBarry Smith 30286c699258SBarry Smith Output Parameter: 30296c699258SBarry Smith . dm - the dm 30306c699258SBarry Smith 30316c699258SBarry Smith Level: intermediate 30326c699258SBarry Smith 30336c699258SBarry Smith 30346c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 30356c699258SBarry Smith @*/ 30367087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 30376c699258SBarry Smith { 3038496e6a7aSJed Brown PetscErrorCode ierr; 3039496e6a7aSJed Brown 30406c699258SBarry Smith PetscFunctionBegin; 30410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3042496e6a7aSJed Brown if (!ts->dm) { 3043496e6a7aSJed Brown ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 3044496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3045496e6a7aSJed Brown } 30466c699258SBarry Smith *dm = ts->dm; 30476c699258SBarry Smith PetscFunctionReturn(0); 30486c699258SBarry Smith } 30491713a123SBarry Smith 30500f5c6efeSJed Brown #undef __FUNCT__ 30510f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 30520f5c6efeSJed Brown /*@ 30530f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 30540f5c6efeSJed Brown 30553f9fe445SBarry Smith Logically Collective on SNES 30560f5c6efeSJed Brown 30570f5c6efeSJed Brown Input Parameter: 3058d42a1c89SJed Brown + snes - nonlinear solver 30590910c330SBarry Smith . U - the current state at which to evaluate the residual 3060d42a1c89SJed Brown - ctx - user context, must be a TS 30610f5c6efeSJed Brown 30620f5c6efeSJed Brown Output Parameter: 30630f5c6efeSJed Brown . F - the nonlinear residual 30640f5c6efeSJed Brown 30650f5c6efeSJed Brown Notes: 30660f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 30670f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 30680f5c6efeSJed Brown 30690f5c6efeSJed Brown Level: advanced 30700f5c6efeSJed Brown 30710f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 30720f5c6efeSJed Brown @*/ 30730910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 30740f5c6efeSJed Brown { 30750f5c6efeSJed Brown TS ts = (TS)ctx; 30760f5c6efeSJed Brown PetscErrorCode ierr; 30770f5c6efeSJed Brown 30780f5c6efeSJed Brown PetscFunctionBegin; 30790f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30800910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 30810f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 30820f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 30830910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 30840f5c6efeSJed Brown PetscFunctionReturn(0); 30850f5c6efeSJed Brown } 30860f5c6efeSJed Brown 30870f5c6efeSJed Brown #undef __FUNCT__ 30880f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 30890f5c6efeSJed Brown /*@ 30900f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 30910f5c6efeSJed Brown 30920f5c6efeSJed Brown Collective on SNES 30930f5c6efeSJed Brown 30940f5c6efeSJed Brown Input Parameter: 30950f5c6efeSJed Brown + snes - nonlinear solver 30960910c330SBarry Smith . U - the current state at which to evaluate the residual 30970f5c6efeSJed Brown - ctx - user context, must be a TS 30980f5c6efeSJed Brown 30990f5c6efeSJed Brown Output Parameter: 31000f5c6efeSJed Brown + A - the Jacobian 31010f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 31020f5c6efeSJed Brown - flag - indicates any structure change in the matrix 31030f5c6efeSJed Brown 31040f5c6efeSJed Brown Notes: 31050f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 31060f5c6efeSJed Brown 31070f5c6efeSJed Brown Level: developer 31080f5c6efeSJed Brown 31090f5c6efeSJed Brown .seealso: SNESSetJacobian() 31100f5c6efeSJed Brown @*/ 31110910c330SBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 31120f5c6efeSJed Brown { 31130f5c6efeSJed Brown TS ts = (TS)ctx; 31140f5c6efeSJed Brown PetscErrorCode ierr; 31150f5c6efeSJed Brown 31160f5c6efeSJed Brown PetscFunctionBegin; 31170f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 31180910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 31190f5c6efeSJed Brown PetscValidPointer(A,3); 31200f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 31210f5c6efeSJed Brown PetscValidPointer(B,4); 31220f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 31230f5c6efeSJed Brown PetscValidPointer(flag,5); 31240f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 31250910c330SBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 31260f5c6efeSJed Brown PetscFunctionReturn(0); 31270f5c6efeSJed Brown } 3128325fc9f4SBarry Smith 31290e4ef248SJed Brown #undef __FUNCT__ 31300e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 31310e4ef248SJed Brown /*@C 31320e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 31330e4ef248SJed Brown 31340e4ef248SJed Brown Collective on TS 31350e4ef248SJed Brown 31360e4ef248SJed Brown Input Arguments: 31370e4ef248SJed Brown + ts - time stepping context 31380e4ef248SJed Brown . t - time at which to evaluate 31390910c330SBarry Smith . U - state at which to evaluate 31400e4ef248SJed Brown - ctx - context 31410e4ef248SJed Brown 31420e4ef248SJed Brown Output Arguments: 31430e4ef248SJed Brown . F - right hand side 31440e4ef248SJed Brown 31450e4ef248SJed Brown Level: intermediate 31460e4ef248SJed Brown 31470e4ef248SJed Brown Notes: 31480e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 31490e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 31500e4ef248SJed Brown 31510e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 31520e4ef248SJed Brown @*/ 31530910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 31540e4ef248SJed Brown { 31550e4ef248SJed Brown PetscErrorCode ierr; 31560e4ef248SJed Brown Mat Arhs,Brhs; 31570e4ef248SJed Brown MatStructure flg2; 31580e4ef248SJed Brown 31590e4ef248SJed Brown PetscFunctionBegin; 31600e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 31610910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 31620910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 31630e4ef248SJed Brown PetscFunctionReturn(0); 31640e4ef248SJed Brown } 31650e4ef248SJed Brown 31660e4ef248SJed Brown #undef __FUNCT__ 31670e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 31680e4ef248SJed Brown /*@C 31690e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 31700e4ef248SJed Brown 31710e4ef248SJed Brown Collective on TS 31720e4ef248SJed Brown 31730e4ef248SJed Brown Input Arguments: 31740e4ef248SJed Brown + ts - time stepping context 31750e4ef248SJed Brown . t - time at which to evaluate 31760910c330SBarry Smith . U - state at which to evaluate 31770e4ef248SJed Brown - ctx - context 31780e4ef248SJed Brown 31790e4ef248SJed Brown Output Arguments: 31800e4ef248SJed Brown + A - pointer to operator 31810e4ef248SJed Brown . B - pointer to preconditioning matrix 31820e4ef248SJed Brown - flg - matrix structure flag 31830e4ef248SJed Brown 31840e4ef248SJed Brown Level: intermediate 31850e4ef248SJed Brown 31860e4ef248SJed Brown Notes: 31870e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 31880e4ef248SJed Brown 31890e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 31900e4ef248SJed Brown @*/ 31910910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 31920e4ef248SJed Brown { 31930e4ef248SJed Brown PetscFunctionBegin; 31940e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 31950e4ef248SJed Brown PetscFunctionReturn(0); 31960e4ef248SJed Brown } 31970e4ef248SJed Brown 31980026cea9SSean Farley #undef __FUNCT__ 31990026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 32000026cea9SSean Farley /*@C 32010026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 32020026cea9SSean Farley 32030026cea9SSean Farley Collective on TS 32040026cea9SSean Farley 32050026cea9SSean Farley Input Arguments: 32060026cea9SSean Farley + ts - time stepping context 32070026cea9SSean Farley . t - time at which to evaluate 32080910c330SBarry Smith . U - state at which to evaluate 32090910c330SBarry Smith . Udot - time derivative of state vector 32100026cea9SSean Farley - ctx - context 32110026cea9SSean Farley 32120026cea9SSean Farley Output Arguments: 32130026cea9SSean Farley . F - left hand side 32140026cea9SSean Farley 32150026cea9SSean Farley Level: intermediate 32160026cea9SSean Farley 32170026cea9SSean Farley Notes: 32180910c330SBarry Smith The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the 32190026cea9SSean Farley user is required to write their own TSComputeIFunction. 32200026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 32210026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 32220026cea9SSean Farley 32230026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 32240026cea9SSean Farley @*/ 32250910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 32260026cea9SSean Farley { 32270026cea9SSean Farley PetscErrorCode ierr; 32280026cea9SSean Farley Mat A,B; 32290026cea9SSean Farley MatStructure flg2; 32300026cea9SSean Farley 32310026cea9SSean Farley PetscFunctionBegin; 32320026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 32330910c330SBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 32340910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 32350026cea9SSean Farley PetscFunctionReturn(0); 32360026cea9SSean Farley } 32370026cea9SSean Farley 32380026cea9SSean Farley #undef __FUNCT__ 32390026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 32400026cea9SSean Farley /*@C 324124e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 32420026cea9SSean Farley 32430026cea9SSean Farley Collective on TS 32440026cea9SSean Farley 32450026cea9SSean Farley Input Arguments: 32460026cea9SSean Farley + ts - time stepping context 32470026cea9SSean Farley . t - time at which to evaluate 32480910c330SBarry Smith . U - state at which to evaluate 32490910c330SBarry Smith . Udot - time derivative of state vector 32500026cea9SSean Farley . shift - shift to apply 32510026cea9SSean Farley - ctx - context 32520026cea9SSean Farley 32530026cea9SSean Farley Output Arguments: 32540026cea9SSean Farley + A - pointer to operator 32550026cea9SSean Farley . B - pointer to preconditioning matrix 32560026cea9SSean Farley - flg - matrix structure flag 32570026cea9SSean Farley 32580026cea9SSean Farley Level: intermediate 32590026cea9SSean Farley 32600026cea9SSean Farley Notes: 32610026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 32620026cea9SSean Farley 32630026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 32640026cea9SSean Farley @*/ 32650910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 32660026cea9SSean Farley { 32670026cea9SSean Farley PetscFunctionBegin; 32680026cea9SSean Farley *flg = SAME_PRECONDITIONER; 32690026cea9SSean Farley PetscFunctionReturn(0); 32700026cea9SSean Farley } 32710026cea9SSean Farley 32724af1b03aSJed Brown #undef __FUNCT__ 32734af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 32744af1b03aSJed Brown /*@ 32754af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 32764af1b03aSJed Brown 32774af1b03aSJed Brown Not Collective 32784af1b03aSJed Brown 32794af1b03aSJed Brown Input Parameter: 32804af1b03aSJed Brown . ts - the TS context 32814af1b03aSJed Brown 32824af1b03aSJed Brown Output Parameter: 32834af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 32844af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 32854af1b03aSJed Brown 3286487e0bb9SJed Brown Level: beginner 32874af1b03aSJed Brown 3288cd652676SJed Brown Notes: 3289cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 32904af1b03aSJed Brown 32914af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 32924af1b03aSJed Brown 32934af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 32944af1b03aSJed Brown @*/ 32954af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 32964af1b03aSJed Brown { 32974af1b03aSJed Brown PetscFunctionBegin; 32984af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32994af1b03aSJed Brown PetscValidPointer(reason,2); 33004af1b03aSJed Brown *reason = ts->reason; 33014af1b03aSJed Brown PetscFunctionReturn(0); 33024af1b03aSJed Brown } 33034af1b03aSJed Brown 3304fb1732b5SBarry Smith #undef __FUNCT__ 3305d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 3306d6ad946cSShri Abhyankar /*@ 3307d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3308d6ad946cSShri Abhyankar 3309d6ad946cSShri Abhyankar Not Collective 3310d6ad946cSShri Abhyankar 3311d6ad946cSShri Abhyankar Input Parameter: 3312d6ad946cSShri Abhyankar + ts - the TS context 3313d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3314d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 3315d6ad946cSShri Abhyankar 3316f5abba47SShri Abhyankar Level: advanced 3317d6ad946cSShri Abhyankar 3318d6ad946cSShri Abhyankar Notes: 3319d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 3320d6ad946cSShri Abhyankar 3321d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 3322d6ad946cSShri Abhyankar 3323d6ad946cSShri Abhyankar .seealso: TSConvergedReason 3324d6ad946cSShri Abhyankar @*/ 3325d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3326d6ad946cSShri Abhyankar { 3327d6ad946cSShri Abhyankar PetscFunctionBegin; 3328d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3329d6ad946cSShri Abhyankar ts->reason = reason; 3330d6ad946cSShri Abhyankar PetscFunctionReturn(0); 3331d6ad946cSShri Abhyankar } 3332d6ad946cSShri Abhyankar 3333d6ad946cSShri Abhyankar #undef __FUNCT__ 3334cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 3335cc708dedSBarry Smith /*@ 3336cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 3337cc708dedSBarry Smith 3338cc708dedSBarry Smith Not Collective 3339cc708dedSBarry Smith 3340cc708dedSBarry Smith Input Parameter: 3341cc708dedSBarry Smith . ts - the TS context 3342cc708dedSBarry Smith 3343cc708dedSBarry Smith Output Parameter: 3344cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3345cc708dedSBarry Smith 3346487e0bb9SJed Brown Level: beginner 3347cc708dedSBarry Smith 3348cc708dedSBarry Smith Notes: 3349cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 3350cc708dedSBarry Smith 3351cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 3352cc708dedSBarry Smith 3353cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 3354cc708dedSBarry Smith @*/ 3355cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3356cc708dedSBarry Smith { 3357cc708dedSBarry Smith PetscFunctionBegin; 3358cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3359cc708dedSBarry Smith PetscValidPointer(ftime,2); 3360cc708dedSBarry Smith *ftime = ts->solvetime; 3361cc708dedSBarry Smith PetscFunctionReturn(0); 3362cc708dedSBarry Smith } 3363cc708dedSBarry Smith 3364cc708dedSBarry Smith #undef __FUNCT__ 33655ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 33669f67acb7SJed Brown /*@ 33675ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 33689f67acb7SJed Brown used by the time integrator. 33699f67acb7SJed Brown 33709f67acb7SJed Brown Not Collective 33719f67acb7SJed Brown 33729f67acb7SJed Brown Input Parameter: 33739f67acb7SJed Brown . ts - TS context 33749f67acb7SJed Brown 33759f67acb7SJed Brown Output Parameter: 33769f67acb7SJed Brown . nits - number of nonlinear iterations 33779f67acb7SJed Brown 33789f67acb7SJed Brown Notes: 33799f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 33809f67acb7SJed Brown 33819f67acb7SJed Brown Level: intermediate 33829f67acb7SJed Brown 33839f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 33849f67acb7SJed Brown 33855ef26d82SJed Brown .seealso: TSGetKSPIterations() 33869f67acb7SJed Brown @*/ 33875ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 33889f67acb7SJed Brown { 33899f67acb7SJed Brown PetscFunctionBegin; 33909f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33919f67acb7SJed Brown PetscValidIntPointer(nits,2); 33925ef26d82SJed Brown *nits = ts->snes_its; 33939f67acb7SJed Brown PetscFunctionReturn(0); 33949f67acb7SJed Brown } 33959f67acb7SJed Brown 33969f67acb7SJed Brown #undef __FUNCT__ 33975ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 33989f67acb7SJed Brown /*@ 33995ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 34009f67acb7SJed Brown used by the time integrator. 34019f67acb7SJed Brown 34029f67acb7SJed Brown Not Collective 34039f67acb7SJed Brown 34049f67acb7SJed Brown Input Parameter: 34059f67acb7SJed Brown . ts - TS context 34069f67acb7SJed Brown 34079f67acb7SJed Brown Output Parameter: 34089f67acb7SJed Brown . lits - number of linear iterations 34099f67acb7SJed Brown 34109f67acb7SJed Brown Notes: 34119f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 34129f67acb7SJed Brown 34139f67acb7SJed Brown Level: intermediate 34149f67acb7SJed Brown 34159f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 34169f67acb7SJed Brown 34175ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 34189f67acb7SJed Brown @*/ 34195ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 34209f67acb7SJed Brown { 34219f67acb7SJed Brown PetscFunctionBegin; 34229f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34239f67acb7SJed Brown PetscValidIntPointer(lits,2); 34245ef26d82SJed Brown *lits = ts->ksp_its; 34259f67acb7SJed Brown PetscFunctionReturn(0); 34269f67acb7SJed Brown } 34279f67acb7SJed Brown 34289f67acb7SJed Brown #undef __FUNCT__ 3429cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3430cef5090cSJed Brown /*@ 3431cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3432cef5090cSJed Brown 3433cef5090cSJed Brown Not Collective 3434cef5090cSJed Brown 3435cef5090cSJed Brown Input Parameter: 3436cef5090cSJed Brown . ts - TS context 3437cef5090cSJed Brown 3438cef5090cSJed Brown Output Parameter: 3439cef5090cSJed Brown . rejects - number of steps rejected 3440cef5090cSJed Brown 3441cef5090cSJed Brown Notes: 3442cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3443cef5090cSJed Brown 3444cef5090cSJed Brown Level: intermediate 3445cef5090cSJed Brown 3446cef5090cSJed Brown .keywords: TS, get, number 3447cef5090cSJed Brown 34485ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3449cef5090cSJed Brown @*/ 3450cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3451cef5090cSJed Brown { 3452cef5090cSJed Brown PetscFunctionBegin; 3453cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3454cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3455cef5090cSJed Brown *rejects = ts->reject; 3456cef5090cSJed Brown PetscFunctionReturn(0); 3457cef5090cSJed Brown } 3458cef5090cSJed Brown 3459cef5090cSJed Brown #undef __FUNCT__ 3460cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3461cef5090cSJed Brown /*@ 3462cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3463cef5090cSJed Brown 3464cef5090cSJed Brown Not Collective 3465cef5090cSJed Brown 3466cef5090cSJed Brown Input Parameter: 3467cef5090cSJed Brown . ts - TS context 3468cef5090cSJed Brown 3469cef5090cSJed Brown Output Parameter: 3470cef5090cSJed Brown . fails - number of failed nonlinear solves 3471cef5090cSJed Brown 3472cef5090cSJed Brown Notes: 3473cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3474cef5090cSJed Brown 3475cef5090cSJed Brown Level: intermediate 3476cef5090cSJed Brown 3477cef5090cSJed Brown .keywords: TS, get, number 3478cef5090cSJed Brown 34795ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3480cef5090cSJed Brown @*/ 3481cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3482cef5090cSJed Brown { 3483cef5090cSJed Brown PetscFunctionBegin; 3484cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3485cef5090cSJed Brown PetscValidIntPointer(fails,2); 3486cef5090cSJed Brown *fails = ts->num_snes_failures; 3487cef5090cSJed Brown PetscFunctionReturn(0); 3488cef5090cSJed Brown } 3489cef5090cSJed Brown 3490cef5090cSJed Brown #undef __FUNCT__ 3491cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3492cef5090cSJed Brown /*@ 3493cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3494cef5090cSJed Brown 3495cef5090cSJed Brown Not Collective 3496cef5090cSJed Brown 3497cef5090cSJed Brown Input Parameter: 3498cef5090cSJed Brown + ts - TS context 3499cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3500cef5090cSJed Brown 3501cef5090cSJed Brown Notes: 3502cef5090cSJed Brown The counter is reset to zero for each step 3503cef5090cSJed Brown 3504cef5090cSJed Brown Options Database Key: 3505cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3506cef5090cSJed Brown 3507cef5090cSJed Brown Level: intermediate 3508cef5090cSJed Brown 3509cef5090cSJed Brown .keywords: TS, set, maximum, number 3510cef5090cSJed Brown 35115ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3512cef5090cSJed Brown @*/ 3513cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3514cef5090cSJed Brown { 3515cef5090cSJed Brown PetscFunctionBegin; 3516cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3517cef5090cSJed Brown ts->max_reject = rejects; 3518cef5090cSJed Brown PetscFunctionReturn(0); 3519cef5090cSJed Brown } 3520cef5090cSJed Brown 3521cef5090cSJed Brown #undef __FUNCT__ 3522cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3523cef5090cSJed Brown /*@ 3524cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3525cef5090cSJed Brown 3526cef5090cSJed Brown Not Collective 3527cef5090cSJed Brown 3528cef5090cSJed Brown Input Parameter: 3529cef5090cSJed Brown + ts - TS context 3530cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3531cef5090cSJed Brown 3532cef5090cSJed Brown Notes: 3533cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3534cef5090cSJed Brown 3535cef5090cSJed Brown Options Database Key: 3536cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3537cef5090cSJed Brown 3538cef5090cSJed Brown Level: intermediate 3539cef5090cSJed Brown 3540cef5090cSJed Brown .keywords: TS, set, maximum, number 3541cef5090cSJed Brown 35425ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3543cef5090cSJed Brown @*/ 3544cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3545cef5090cSJed Brown { 3546cef5090cSJed Brown PetscFunctionBegin; 3547cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3548cef5090cSJed Brown ts->max_snes_failures = fails; 3549cef5090cSJed Brown PetscFunctionReturn(0); 3550cef5090cSJed Brown } 3551cef5090cSJed Brown 3552cef5090cSJed Brown #undef __FUNCT__ 3553cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3554cef5090cSJed Brown /*@ 3555cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3556cef5090cSJed Brown 3557cef5090cSJed Brown Not Collective 3558cef5090cSJed Brown 3559cef5090cSJed Brown Input Parameter: 3560cef5090cSJed Brown + ts - TS context 3561cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3562cef5090cSJed Brown 3563cef5090cSJed Brown Options Database Key: 3564cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3565cef5090cSJed Brown 3566cef5090cSJed Brown Level: intermediate 3567cef5090cSJed Brown 3568cef5090cSJed Brown .keywords: TS, set, error 3569cef5090cSJed Brown 35705ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3571cef5090cSJed Brown @*/ 3572cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3573cef5090cSJed Brown { 3574cef5090cSJed Brown PetscFunctionBegin; 3575cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3576cef5090cSJed Brown ts->errorifstepfailed = err; 3577cef5090cSJed Brown PetscFunctionReturn(0); 3578cef5090cSJed Brown } 3579cef5090cSJed Brown 3580cef5090cSJed Brown #undef __FUNCT__ 3581fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3582fb1732b5SBarry Smith /*@C 3583fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3584fb1732b5SBarry Smith 3585fb1732b5SBarry Smith Collective on TS 3586fb1732b5SBarry Smith 3587fb1732b5SBarry Smith Input Parameters: 3588fb1732b5SBarry Smith + ts - the TS context 3589fb1732b5SBarry Smith . step - current time-step 3590fb1732b5SBarry Smith . ptime - current time 35910910c330SBarry Smith . u - current state 3592fb1732b5SBarry Smith - viewer - binary viewer 3593fb1732b5SBarry Smith 3594fb1732b5SBarry Smith Level: intermediate 3595fb1732b5SBarry Smith 3596fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3597fb1732b5SBarry Smith 3598fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3599fb1732b5SBarry Smith @*/ 36000910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3601fb1732b5SBarry Smith { 3602fb1732b5SBarry Smith PetscErrorCode ierr; 3603ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3604fb1732b5SBarry Smith 3605fb1732b5SBarry Smith PetscFunctionBegin; 36060910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 3607ed81e22dSJed Brown PetscFunctionReturn(0); 3608ed81e22dSJed Brown } 3609ed81e22dSJed Brown 3610ed81e22dSJed Brown #undef __FUNCT__ 3611ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3612ed81e22dSJed Brown /*@C 3613ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3614ed81e22dSJed Brown 3615ed81e22dSJed Brown Collective on TS 3616ed81e22dSJed Brown 3617ed81e22dSJed Brown Input Parameters: 3618ed81e22dSJed Brown + ts - the TS context 3619ed81e22dSJed Brown . step - current time-step 3620ed81e22dSJed Brown . ptime - current time 36210910c330SBarry Smith . u - current state 3622ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3623ed81e22dSJed Brown 3624ed81e22dSJed Brown Level: intermediate 3625ed81e22dSJed Brown 3626ed81e22dSJed Brown Notes: 3627ed81e22dSJed 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. 3628ed81e22dSJed Brown These are named according to the file name template. 3629ed81e22dSJed Brown 3630ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3631ed81e22dSJed Brown 3632ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3633ed81e22dSJed Brown 3634ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3635ed81e22dSJed Brown @*/ 36360910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3637ed81e22dSJed Brown { 3638ed81e22dSJed Brown PetscErrorCode ierr; 3639ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3640ed81e22dSJed Brown PetscViewer viewer; 3641ed81e22dSJed Brown 3642ed81e22dSJed Brown PetscFunctionBegin; 36438caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3644ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 36450910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 3646ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3647ed81e22dSJed Brown PetscFunctionReturn(0); 3648ed81e22dSJed Brown } 3649ed81e22dSJed Brown 3650ed81e22dSJed Brown #undef __FUNCT__ 3651ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3652ed81e22dSJed Brown /*@C 3653ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3654ed81e22dSJed Brown 3655ed81e22dSJed Brown Collective on TS 3656ed81e22dSJed Brown 3657ed81e22dSJed Brown Input Parameters: 3658ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3659ed81e22dSJed Brown 3660ed81e22dSJed Brown Level: intermediate 3661ed81e22dSJed Brown 3662ed81e22dSJed Brown Note: 3663ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3664ed81e22dSJed Brown 3665ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3666ed81e22dSJed Brown 3667ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3668ed81e22dSJed Brown @*/ 3669ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3670ed81e22dSJed Brown { 3671ed81e22dSJed Brown PetscErrorCode ierr; 3672ed81e22dSJed Brown 3673ed81e22dSJed Brown PetscFunctionBegin; 3674ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3675fb1732b5SBarry Smith PetscFunctionReturn(0); 3676fb1732b5SBarry Smith } 3677fb1732b5SBarry Smith 367884df9cb4SJed Brown #undef __FUNCT__ 3679ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt" 368084df9cb4SJed Brown /*@ 3681ad6bc421SBarry Smith TSGetTSAdapt - Get the adaptive controller context for the current method 368284df9cb4SJed Brown 3683ed81e22dSJed Brown Collective on TS if controller has not been created yet 368484df9cb4SJed Brown 368584df9cb4SJed Brown Input Arguments: 3686ed81e22dSJed Brown . ts - time stepping context 368784df9cb4SJed Brown 368884df9cb4SJed Brown Output Arguments: 3689ed81e22dSJed Brown . adapt - adaptive controller 369084df9cb4SJed Brown 369184df9cb4SJed Brown Level: intermediate 369284df9cb4SJed Brown 3693ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 369484df9cb4SJed Brown @*/ 3695ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt) 369684df9cb4SJed Brown { 369784df9cb4SJed Brown PetscErrorCode ierr; 369884df9cb4SJed Brown 369984df9cb4SJed Brown PetscFunctionBegin; 370084df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 370184df9cb4SJed Brown PetscValidPointer(adapt,2); 370284df9cb4SJed Brown if (!ts->adapt) { 370384df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 37041c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 37051c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 370684df9cb4SJed Brown } 370784df9cb4SJed Brown *adapt = ts->adapt; 370884df9cb4SJed Brown PetscFunctionReturn(0); 370984df9cb4SJed Brown } 3710d6ebe24aSShri Abhyankar 3711d6ebe24aSShri Abhyankar #undef __FUNCT__ 37121c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 37131c3436cfSJed Brown /*@ 37141c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 37151c3436cfSJed Brown 37161c3436cfSJed Brown Logically Collective 37171c3436cfSJed Brown 37181c3436cfSJed Brown Input Arguments: 37191c3436cfSJed Brown + ts - time integration context 37201c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 37211c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 37221c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 37231c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 37241c3436cfSJed Brown 37251c3436cfSJed Brown Level: beginner 37261c3436cfSJed Brown 3727c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 37281c3436cfSJed Brown @*/ 37291c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 37301c3436cfSJed Brown { 37311c3436cfSJed Brown PetscErrorCode ierr; 37321c3436cfSJed Brown 37331c3436cfSJed Brown PetscFunctionBegin; 3734c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 37351c3436cfSJed Brown if (vatol) { 37361c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 37371c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 37381c3436cfSJed Brown ts->vatol = vatol; 37391c3436cfSJed Brown } 3740c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 37411c3436cfSJed Brown if (vrtol) { 37421c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 37431c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 37441c3436cfSJed Brown ts->vrtol = vrtol; 37451c3436cfSJed Brown } 37461c3436cfSJed Brown PetscFunctionReturn(0); 37471c3436cfSJed Brown } 37481c3436cfSJed Brown 37491c3436cfSJed Brown #undef __FUNCT__ 3750c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 3751c5033834SJed Brown /*@ 3752c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3753c5033834SJed Brown 3754c5033834SJed Brown Logically Collective 3755c5033834SJed Brown 3756c5033834SJed Brown Input Arguments: 3757c5033834SJed Brown . ts - time integration context 3758c5033834SJed Brown 3759c5033834SJed Brown Output Arguments: 3760c5033834SJed Brown + atol - scalar absolute tolerances, PETSC_NULL to ignore 3761c5033834SJed Brown . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3762c5033834SJed Brown . rtol - scalar relative tolerances, PETSC_NULL to ignore 3763c5033834SJed Brown - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3764c5033834SJed Brown 3765c5033834SJed Brown Level: beginner 3766c5033834SJed Brown 3767c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3768c5033834SJed Brown @*/ 3769c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3770c5033834SJed Brown { 3771c5033834SJed Brown PetscFunctionBegin; 3772c5033834SJed Brown if (atol) *atol = ts->atol; 3773c5033834SJed Brown if (vatol) *vatol = ts->vatol; 3774c5033834SJed Brown if (rtol) *rtol = ts->rtol; 3775c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 3776c5033834SJed Brown PetscFunctionReturn(0); 3777c5033834SJed Brown } 3778c5033834SJed Brown 3779c5033834SJed Brown #undef __FUNCT__ 37801c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 37811c3436cfSJed Brown /*@ 37821c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 37831c3436cfSJed Brown 37841c3436cfSJed Brown Collective on TS 37851c3436cfSJed Brown 37861c3436cfSJed Brown Input Arguments: 37871c3436cfSJed Brown + ts - time stepping context 37881c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 37891c3436cfSJed Brown 37901c3436cfSJed Brown Output Arguments: 37911c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 37921c3436cfSJed Brown 37931c3436cfSJed Brown Level: developer 37941c3436cfSJed Brown 37951c3436cfSJed Brown .seealso: TSSetTolerances() 37961c3436cfSJed Brown @*/ 37971c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 37981c3436cfSJed Brown { 37991c3436cfSJed Brown PetscErrorCode ierr; 38001c3436cfSJed Brown PetscInt i,n,N; 38010910c330SBarry Smith const PetscScalar *u,*y; 38020910c330SBarry Smith Vec U; 38031c3436cfSJed Brown PetscReal sum,gsum; 38041c3436cfSJed Brown 38051c3436cfSJed Brown PetscFunctionBegin; 38061c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38071c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 38081c3436cfSJed Brown PetscValidPointer(norm,3); 38090910c330SBarry Smith U = ts->vec_sol; 38100910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 38110910c330SBarry Smith if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 38121c3436cfSJed Brown 38130910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 38140910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 38150910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 38161c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 38171c3436cfSJed Brown sum = 0.; 3818ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 3819ceefc113SJed Brown const PetscScalar *atol,*rtol; 3820ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3821ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3822ceefc113SJed Brown for (i=0; i<n; i++) { 38230910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38240910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3825ceefc113SJed Brown } 3826ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3827ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3828ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 3829ceefc113SJed Brown const PetscScalar *atol; 3830ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3831ceefc113SJed Brown for (i=0; i<n; i++) { 38320910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38330910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3834ceefc113SJed Brown } 3835ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3836ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3837ceefc113SJed Brown const PetscScalar *rtol; 3838ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3839ceefc113SJed Brown for (i=0; i<n; i++) { 38400910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38410910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3842ceefc113SJed Brown } 3843ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3844ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 38451c3436cfSJed Brown for (i=0; i<n; i++) { 38460910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38470910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 38481c3436cfSJed Brown } 3849ceefc113SJed Brown } 38500910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 38511c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 38521c3436cfSJed Brown 38531c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 38541c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 38551c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 38561c3436cfSJed Brown PetscFunctionReturn(0); 38571c3436cfSJed Brown } 38581c3436cfSJed Brown 38591c3436cfSJed Brown #undef __FUNCT__ 38608d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 38618d59e960SJed Brown /*@ 38628d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 38638d59e960SJed Brown 38648d59e960SJed Brown Logically Collective on TS 38658d59e960SJed Brown 38668d59e960SJed Brown Input Arguments: 38678d59e960SJed Brown + ts - time stepping context 38688d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 38698d59e960SJed Brown 38708d59e960SJed Brown Note: 38718d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 38728d59e960SJed Brown 38738d59e960SJed Brown Level: intermediate 38748d59e960SJed Brown 38758d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 38768d59e960SJed Brown @*/ 38778d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 38788d59e960SJed Brown { 38798d59e960SJed Brown PetscFunctionBegin; 38808d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38818d59e960SJed Brown ts->cfltime_local = cfltime; 38828d59e960SJed Brown ts->cfltime = -1.; 38838d59e960SJed Brown PetscFunctionReturn(0); 38848d59e960SJed Brown } 38858d59e960SJed Brown 38868d59e960SJed Brown #undef __FUNCT__ 38878d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 38888d59e960SJed Brown /*@ 38898d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 38908d59e960SJed Brown 38918d59e960SJed Brown Collective on TS 38928d59e960SJed Brown 38938d59e960SJed Brown Input Arguments: 38948d59e960SJed Brown . ts - time stepping context 38958d59e960SJed Brown 38968d59e960SJed Brown Output Arguments: 38978d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 38988d59e960SJed Brown 38998d59e960SJed Brown Level: advanced 39008d59e960SJed Brown 39018d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 39028d59e960SJed Brown @*/ 39038d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 39048d59e960SJed Brown { 39058d59e960SJed Brown PetscErrorCode ierr; 39068d59e960SJed Brown 39078d59e960SJed Brown PetscFunctionBegin; 39088d59e960SJed Brown if (ts->cfltime < 0) { 39098d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 39108d59e960SJed Brown } 39118d59e960SJed Brown *cfltime = ts->cfltime; 39128d59e960SJed Brown PetscFunctionReturn(0); 39138d59e960SJed Brown } 39148d59e960SJed Brown 39158d59e960SJed Brown #undef __FUNCT__ 3916d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3917d6ebe24aSShri Abhyankar /*@ 3918d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3919d6ebe24aSShri Abhyankar 3920d6ebe24aSShri Abhyankar Input Parameters: 3921d6ebe24aSShri Abhyankar . ts - the TS context. 3922d6ebe24aSShri Abhyankar . xl - lower bound. 3923d6ebe24aSShri Abhyankar . xu - upper bound. 3924d6ebe24aSShri Abhyankar 3925d6ebe24aSShri Abhyankar Notes: 3926d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 392733c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3928d6ebe24aSShri Abhyankar 39292bd2b0e6SSatish Balay Level: advanced 39302bd2b0e6SSatish Balay 3931d6ebe24aSShri Abhyankar @*/ 3932d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3933d6ebe24aSShri Abhyankar { 3934d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3935d6ebe24aSShri Abhyankar SNES snes; 3936d6ebe24aSShri Abhyankar 3937d6ebe24aSShri Abhyankar PetscFunctionBegin; 3938d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3939d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3940d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3941d6ebe24aSShri Abhyankar } 3942d6ebe24aSShri Abhyankar 3943325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3944c6db04a5SJed Brown #include <mex.h> 3945325fc9f4SBarry Smith 3946325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3947325fc9f4SBarry Smith 3948325fc9f4SBarry Smith #undef __FUNCT__ 3949325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3950325fc9f4SBarry Smith /* 3951325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3952325fc9f4SBarry Smith TSSetFunctionMatlab(). 3953325fc9f4SBarry Smith 3954325fc9f4SBarry Smith Collective on TS 3955325fc9f4SBarry Smith 3956325fc9f4SBarry Smith Input Parameters: 3957325fc9f4SBarry Smith + snes - the TS context 39580910c330SBarry Smith - u - input vector 3959325fc9f4SBarry Smith 3960325fc9f4SBarry Smith Output Parameter: 3961325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3962325fc9f4SBarry Smith 3963325fc9f4SBarry Smith Notes: 3964325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3965325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3966325fc9f4SBarry Smith themselves. 3967325fc9f4SBarry Smith 3968325fc9f4SBarry Smith Level: developer 3969325fc9f4SBarry Smith 3970325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3971325fc9f4SBarry Smith 3972325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3973325fc9f4SBarry Smith */ 39740910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 3975325fc9f4SBarry Smith { 3976325fc9f4SBarry Smith PetscErrorCode ierr; 3977325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3978325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3979325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3980325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3981325fc9f4SBarry Smith 3982325fc9f4SBarry Smith PetscFunctionBegin; 3983325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 39840910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 39850910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 3986325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 39870910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 3988325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3989325fc9f4SBarry Smith 3990325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 39910910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 39920910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 39930910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 3994325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3995325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3996325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3997325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3998325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3999325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 4000325fc9f4SBarry Smith prhs[6] = sctx->ctx; 4001325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 4002325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4003325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4004325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4005325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4006325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4007325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4008325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4009325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4010325fc9f4SBarry Smith PetscFunctionReturn(0); 4011325fc9f4SBarry Smith } 4012325fc9f4SBarry Smith 4013325fc9f4SBarry Smith 4014325fc9f4SBarry Smith #undef __FUNCT__ 4015325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 4016325fc9f4SBarry Smith /* 4017325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 4018325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 4019e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 4020325fc9f4SBarry Smith 4021325fc9f4SBarry Smith Logically Collective on TS 4022325fc9f4SBarry Smith 4023325fc9f4SBarry Smith Input Parameters: 4024325fc9f4SBarry Smith + ts - the TS context 4025325fc9f4SBarry Smith - func - function evaluation routine 4026325fc9f4SBarry Smith 4027325fc9f4SBarry Smith Calling sequence of func: 40280910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4029325fc9f4SBarry Smith 4030325fc9f4SBarry Smith Level: beginner 4031325fc9f4SBarry Smith 4032325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4033325fc9f4SBarry Smith 4034325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4035325fc9f4SBarry Smith */ 4036cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4037325fc9f4SBarry Smith { 4038325fc9f4SBarry Smith PetscErrorCode ierr; 4039325fc9f4SBarry Smith TSMatlabContext *sctx; 4040325fc9f4SBarry Smith 4041325fc9f4SBarry Smith PetscFunctionBegin; 4042325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4043325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4044325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4045325fc9f4SBarry Smith /* 4046325fc9f4SBarry Smith This should work, but it doesn't 4047325fc9f4SBarry Smith sctx->ctx = ctx; 4048325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4049325fc9f4SBarry Smith */ 4050325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4051cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4052325fc9f4SBarry Smith PetscFunctionReturn(0); 4053325fc9f4SBarry Smith } 4054325fc9f4SBarry Smith 4055325fc9f4SBarry Smith #undef __FUNCT__ 4056325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 4057325fc9f4SBarry Smith /* 4058325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 4059325fc9f4SBarry Smith TSSetJacobianMatlab(). 4060325fc9f4SBarry Smith 4061325fc9f4SBarry Smith Collective on TS 4062325fc9f4SBarry Smith 4063325fc9f4SBarry Smith Input Parameters: 4064cdcf91faSSean Farley + ts - the TS context 40650910c330SBarry Smith . u - input vector 4066325fc9f4SBarry Smith . A, B - the matrices 4067325fc9f4SBarry Smith - ctx - user context 4068325fc9f4SBarry Smith 4069325fc9f4SBarry Smith Output Parameter: 4070325fc9f4SBarry Smith . flag - structure of the matrix 4071325fc9f4SBarry Smith 4072325fc9f4SBarry Smith Level: developer 4073325fc9f4SBarry Smith 4074325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4075325fc9f4SBarry Smith 4076325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4077325fc9f4SBarry Smith @*/ 40780910c330SBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 4079325fc9f4SBarry Smith { 4080325fc9f4SBarry Smith PetscErrorCode ierr; 4081325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4082325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 4083325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 4084325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4085325fc9f4SBarry Smith 4086325fc9f4SBarry Smith PetscFunctionBegin; 4087cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40880910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4089325fc9f4SBarry Smith 40900910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 4091325fc9f4SBarry Smith 4092cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 40930910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 40940910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 40950910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 40960910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4097325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4098325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 4099325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4100325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4101325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 4102325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 4103325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 4104325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 4105325fc9f4SBarry Smith prhs[8] = sctx->ctx; 4106325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4107325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4108325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4109325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4110325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4111325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4112325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4113325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4114325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4115325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 4116325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 4117325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4118325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 4119325fc9f4SBarry Smith PetscFunctionReturn(0); 4120325fc9f4SBarry Smith } 4121325fc9f4SBarry Smith 4122325fc9f4SBarry Smith 4123325fc9f4SBarry Smith #undef __FUNCT__ 4124325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 4125325fc9f4SBarry Smith /* 4126325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4127e3c5b3baSBarry 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 4128325fc9f4SBarry Smith 4129325fc9f4SBarry Smith Logically Collective on TS 4130325fc9f4SBarry Smith 4131325fc9f4SBarry Smith Input Parameters: 4132cdcf91faSSean Farley + ts - the TS context 4133325fc9f4SBarry Smith . A,B - Jacobian matrices 4134325fc9f4SBarry Smith . func - function evaluation routine 4135325fc9f4SBarry Smith - ctx - user context 4136325fc9f4SBarry Smith 4137325fc9f4SBarry Smith Calling sequence of func: 41380910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4139325fc9f4SBarry Smith 4140325fc9f4SBarry Smith 4141325fc9f4SBarry Smith Level: developer 4142325fc9f4SBarry Smith 4143325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4144325fc9f4SBarry Smith 4145325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4146325fc9f4SBarry Smith */ 4147cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4148325fc9f4SBarry Smith { 4149325fc9f4SBarry Smith PetscErrorCode ierr; 4150325fc9f4SBarry Smith TSMatlabContext *sctx; 4151325fc9f4SBarry Smith 4152325fc9f4SBarry Smith PetscFunctionBegin; 4153325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4154325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4155325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4156325fc9f4SBarry Smith /* 4157325fc9f4SBarry Smith This should work, but it doesn't 4158325fc9f4SBarry Smith sctx->ctx = ctx; 4159325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4160325fc9f4SBarry Smith */ 4161325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4162cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4163325fc9f4SBarry Smith PetscFunctionReturn(0); 4164325fc9f4SBarry Smith } 4165325fc9f4SBarry Smith 4166b5b1a830SBarry Smith #undef __FUNCT__ 4167b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 4168b5b1a830SBarry Smith /* 4169b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4170b5b1a830SBarry Smith 4171b5b1a830SBarry Smith Collective on TS 4172b5b1a830SBarry Smith 4173b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4174b5b1a830SBarry Smith @*/ 41750910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4176b5b1a830SBarry Smith { 4177b5b1a830SBarry Smith PetscErrorCode ierr; 4178b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4179a530c242SBarry Smith int nlhs = 1,nrhs = 6; 4180b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 4181b5b1a830SBarry Smith long long int lx = 0,ls = 0; 4182b5b1a830SBarry Smith 4183b5b1a830SBarry Smith PetscFunctionBegin; 4184cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41850910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4186b5b1a830SBarry Smith 4187cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 41880910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4189b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4190b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 4191b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 4192b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 4193b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4194b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4195b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4196b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4197b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4198b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4199b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4200b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4201b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4202b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4203b5b1a830SBarry Smith PetscFunctionReturn(0); 4204b5b1a830SBarry Smith } 4205b5b1a830SBarry Smith 4206b5b1a830SBarry Smith 4207b5b1a830SBarry Smith #undef __FUNCT__ 4208b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4209b5b1a830SBarry Smith /* 4210b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4211b5b1a830SBarry Smith 4212b5b1a830SBarry Smith Level: developer 4213b5b1a830SBarry Smith 4214b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4215b5b1a830SBarry Smith 4216b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4217b5b1a830SBarry Smith */ 4218cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4219b5b1a830SBarry Smith { 4220b5b1a830SBarry Smith PetscErrorCode ierr; 4221b5b1a830SBarry Smith TSMatlabContext *sctx; 4222b5b1a830SBarry Smith 4223b5b1a830SBarry Smith PetscFunctionBegin; 4224b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4225b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4226b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4227b5b1a830SBarry Smith /* 4228b5b1a830SBarry Smith This should work, but it doesn't 4229b5b1a830SBarry Smith sctx->ctx = ctx; 4230b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4231b5b1a830SBarry Smith */ 4232b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4233cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4234b5b1a830SBarry Smith PetscFunctionReturn(0); 4235b5b1a830SBarry Smith } 4236325fc9f4SBarry Smith #endif 4237b3603a34SBarry Smith 42380b039ecaSBarry Smith 42390b039ecaSBarry Smith 4240b3603a34SBarry Smith #undef __FUNCT__ 42414f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4242b3603a34SBarry Smith /*@C 42434f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4244b3603a34SBarry Smith in a time based line graph 4245b3603a34SBarry Smith 4246b3603a34SBarry Smith Collective on TS 4247b3603a34SBarry Smith 4248b3603a34SBarry Smith Input Parameters: 4249b3603a34SBarry Smith + ts - the TS context 4250b3603a34SBarry Smith . step - current time-step 4251b3603a34SBarry Smith . ptime - current time 4252b3603a34SBarry Smith - lg - a line graph object 4253b3603a34SBarry Smith 4254b3603a34SBarry Smith Level: intermediate 4255b3603a34SBarry Smith 42560b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4257b3603a34SBarry Smith 4258b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4259b3603a34SBarry Smith 4260b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4261b3603a34SBarry Smith @*/ 42620910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4263b3603a34SBarry Smith { 4264b3603a34SBarry Smith PetscErrorCode ierr; 42650b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4266b3603a34SBarry Smith const PetscScalar *yy; 426758ff32f7SBarry Smith PetscInt dim; 4268b3603a34SBarry Smith 4269b3603a34SBarry Smith PetscFunctionBegin; 427058ff32f7SBarry Smith if (!step) { 4271a9f9c1f6SBarry Smith PetscDrawAxis axis; 4272a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4273a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 42740910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 42750b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 42760b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 427758ff32f7SBarry Smith } 42780910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4279e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4280e3efe391SJed Brown { 4281e3efe391SJed Brown PetscReal *yreal; 4282e3efe391SJed Brown PetscInt i,n; 42830910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4284e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4285e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 42860b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4287e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4288e3efe391SJed Brown } 4289e3efe391SJed Brown #else 42900b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4291e3efe391SJed Brown #endif 42920910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 42933fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 42940b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 42953923b477SBarry Smith } 4296b3603a34SBarry Smith PetscFunctionReturn(0); 4297b3603a34SBarry Smith } 4298b3603a34SBarry Smith 4299b3603a34SBarry Smith #undef __FUNCT__ 43004f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4301ef20d060SBarry Smith /*@C 43024f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4303ef20d060SBarry Smith in a time based line graph 4304ef20d060SBarry Smith 4305ef20d060SBarry Smith Collective on TS 4306ef20d060SBarry Smith 4307ef20d060SBarry Smith Input Parameters: 4308ef20d060SBarry Smith + ts - the TS context 4309ef20d060SBarry Smith . step - current time-step 4310ef20d060SBarry Smith . ptime - current time 4311ef20d060SBarry Smith - lg - a line graph object 4312ef20d060SBarry Smith 4313ef20d060SBarry Smith Level: intermediate 4314ef20d060SBarry Smith 4315abd5a294SJed Brown Notes: 4316abd5a294SJed Brown Only for sequential solves. 4317abd5a294SJed Brown 4318abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4319abd5a294SJed Brown 4320abd5a294SJed Brown Options Database Keys: 43214f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4322ef20d060SBarry Smith 4323ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4324ef20d060SBarry Smith 4325abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4326ef20d060SBarry Smith @*/ 43270910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4328ef20d060SBarry Smith { 4329ef20d060SBarry Smith PetscErrorCode ierr; 43300b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4331ef20d060SBarry Smith const PetscScalar *yy; 4332ef20d060SBarry Smith Vec y; 4333a9f9c1f6SBarry Smith PetscInt dim; 4334ef20d060SBarry Smith 4335ef20d060SBarry Smith PetscFunctionBegin; 4336a9f9c1f6SBarry Smith if (!step) { 4337a9f9c1f6SBarry Smith PetscDrawAxis axis; 4338a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 43391ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 43400910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4341a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4342a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4343a9f9c1f6SBarry Smith } 43440910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4345ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 43460910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4347ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4348e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4349e3efe391SJed Brown { 4350e3efe391SJed Brown PetscReal *yreal; 4351e3efe391SJed Brown PetscInt i,n; 4352e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4353e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4354e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 43550b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4356e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4357e3efe391SJed Brown } 4358e3efe391SJed Brown #else 43590b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4360e3efe391SJed Brown #endif 4361ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4362ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 43633fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 43640b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 43653923b477SBarry Smith } 4366ef20d060SBarry Smith PetscFunctionReturn(0); 4367ef20d060SBarry Smith } 4368ef20d060SBarry Smith 4369201da799SBarry Smith #undef __FUNCT__ 4370201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4371201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4372201da799SBarry Smith { 4373201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4374201da799SBarry Smith PetscReal x = ptime,y; 4375201da799SBarry Smith PetscErrorCode ierr; 4376201da799SBarry Smith PetscInt its; 4377201da799SBarry Smith 4378201da799SBarry Smith PetscFunctionBegin; 4379201da799SBarry Smith if (!n) { 4380201da799SBarry Smith PetscDrawAxis axis; 4381201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4382201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4383201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4384201da799SBarry Smith ctx->snes_its = 0; 4385201da799SBarry Smith } 4386201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4387201da799SBarry Smith y = its - ctx->snes_its; 4388201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 43893fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4390201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4391201da799SBarry Smith } 4392201da799SBarry Smith ctx->snes_its = its; 4393201da799SBarry Smith PetscFunctionReturn(0); 4394201da799SBarry Smith } 4395201da799SBarry Smith 4396201da799SBarry Smith #undef __FUNCT__ 4397201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4398201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4399201da799SBarry Smith { 4400201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4401201da799SBarry Smith PetscReal x = ptime,y; 4402201da799SBarry Smith PetscErrorCode ierr; 4403201da799SBarry Smith PetscInt its; 4404201da799SBarry Smith 4405201da799SBarry Smith PetscFunctionBegin; 4406201da799SBarry Smith if (!n) { 4407201da799SBarry Smith PetscDrawAxis axis; 4408201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4409201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4410201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4411201da799SBarry Smith ctx->ksp_its = 0; 4412201da799SBarry Smith } 4413201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4414201da799SBarry Smith y = its - ctx->ksp_its; 4415201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 441699fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4417201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4418201da799SBarry Smith } 4419201da799SBarry Smith ctx->ksp_its = its; 4420201da799SBarry Smith PetscFunctionReturn(0); 4421201da799SBarry Smith } 4422f9c1d6abSBarry Smith 4423f9c1d6abSBarry Smith #undef __FUNCT__ 4424f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 4425f9c1d6abSBarry Smith /*@ 4426f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 4427f9c1d6abSBarry Smith 4428f9c1d6abSBarry Smith Collective on TS and Vec 4429f9c1d6abSBarry Smith 4430f9c1d6abSBarry Smith Input Parameters: 4431f9c1d6abSBarry Smith + ts - the TS context 4432f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 4433f9c1d6abSBarry Smith 4434f9c1d6abSBarry Smith Output Parameters: 4435f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 4436f9c1d6abSBarry Smith 4437f9c1d6abSBarry Smith Level: developer 4438f9c1d6abSBarry Smith 4439f9c1d6abSBarry Smith .keywords: TS, compute 4440f9c1d6abSBarry Smith 4441f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 4442f9c1d6abSBarry Smith @*/ 4443f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4444f9c1d6abSBarry Smith { 4445f9c1d6abSBarry Smith PetscErrorCode ierr; 4446f9c1d6abSBarry Smith 4447f9c1d6abSBarry Smith PetscFunctionBegin; 4448f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4449f9c1d6abSBarry Smith if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4450f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4451f9c1d6abSBarry Smith PetscFunctionReturn(0); 4452f9c1d6abSBarry Smith } 4453