17cb94ee6SHong Zhang /* 2db268bfaSHong Zhang Provides a PETSc interface to SUNDIALS/CVODE solver. 37cb94ee6SHong Zhang The interface to PVODE (old version of CVODE) was originally contributed 47cb94ee6SHong Zhang by Liyang Xu. It has been redone by Hong Zhang and Dinesh Kaushik. 528adb3f7SHong Zhang 628adb3f7SHong Zhang Reference: sundials-2.4.0/examples/cvode/parallel/cvDiurnal_kry_p.c 77cb94ee6SHong Zhang */ 81c7d2463SJed Brown #include <../src/ts/impls/implicit/sundials/sundials.h> /*I "petscts.h" I*/ 97cb94ee6SHong Zhang 107cb94ee6SHong Zhang /* 117cb94ee6SHong Zhang TSPrecond_Sundials - function that we provide to SUNDIALS to 127cb94ee6SHong Zhang evaluate the preconditioner. 137cb94ee6SHong Zhang */ 14bbd56ea5SKarl Rupp PetscErrorCode TSPrecond_Sundials(realtype tn,N_Vector y,N_Vector fy,booleantype jok,booleantype *jcurPtr, 15bbd56ea5SKarl Rupp realtype _gamma,void *P_data,N_Vector vtemp1,N_Vector vtemp2,N_Vector vtemp3) 167cb94ee6SHong Zhang { 177cb94ee6SHong Zhang TS ts = (TS) P_data; 187cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 19dcbc6d53SSean Farley PC pc; 207cb94ee6SHong Zhang PetscErrorCode ierr; 210679a0aeSJed Brown Mat J,P; 220679a0aeSJed Brown Vec yy = cvode->w1,yydot = cvode->ydot; 230679a0aeSJed Brown PetscReal gm = (PetscReal)_gamma; 247cb94ee6SHong Zhang PetscScalar *y_data; 257cb94ee6SHong Zhang 267cb94ee6SHong Zhang PetscFunctionBegin; 270298fd71SBarry Smith ierr = TSGetIJacobian(ts,&J,&P,NULL,NULL);CHKERRQ(ierr); 287cb94ee6SHong Zhang y_data = (PetscScalar*) N_VGetArrayPointer(y); 297cb94ee6SHong Zhang ierr = VecPlaceArray(yy,y_data);CHKERRQ(ierr); 300679a0aeSJed Brown ierr = VecZeroEntries(yydot);CHKERRQ(ierr); /* The Jacobian is independent of Ydot for ODE which is all that CVode works for */ 310679a0aeSJed Brown /* compute the shifted Jacobian (1/gm)*I + Jrest */ 3209e82e2bSBarry Smith ierr = TSComputeIJacobian(ts,ts->ptime,yy,yydot,1/gm,J,P,PETSC_FALSE);CHKERRQ(ierr); 337cb94ee6SHong Zhang ierr = VecResetArray(yy);CHKERRQ(ierr); 340679a0aeSJed Brown ierr = MatScale(P,gm);CHKERRQ(ierr); /* turn into I-gm*Jrest, J is not used by Sundials */ 357cb94ee6SHong Zhang *jcurPtr = TRUE; 36dcbc6d53SSean Farley ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr); 3709e82e2bSBarry Smith ierr = PCSetOperators(pc,J,P);CHKERRQ(ierr); 387cb94ee6SHong Zhang PetscFunctionReturn(0); 397cb94ee6SHong Zhang } 407cb94ee6SHong Zhang 417cb94ee6SHong Zhang /* 427cb94ee6SHong Zhang TSPSolve_Sundials - routine that we provide to Sundials that applies the preconditioner. 437cb94ee6SHong Zhang */ 444ac7836bSHong Zhang PetscErrorCode TSPSolve_Sundials(realtype tn,N_Vector y,N_Vector fy,N_Vector r,N_Vector z, 454ac7836bSHong Zhang realtype _gamma,realtype delta,int lr,void *P_data,N_Vector vtemp) 467cb94ee6SHong Zhang { 477cb94ee6SHong Zhang TS ts = (TS) P_data; 487cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 49dcbc6d53SSean Farley PC pc; 507cb94ee6SHong Zhang Vec rr = cvode->w1,zz = cvode->w2; 517cb94ee6SHong Zhang PetscErrorCode ierr; 527cb94ee6SHong Zhang PetscScalar *r_data,*z_data; 537cb94ee6SHong Zhang 547cb94ee6SHong Zhang PetscFunctionBegin; 557cb94ee6SHong Zhang /* Make the PETSc work vectors rr and zz point to the arrays in the SUNDIALS vectors r and z respectively*/ 567cb94ee6SHong Zhang r_data = (PetscScalar*) N_VGetArrayPointer(r); 577cb94ee6SHong Zhang z_data = (PetscScalar*) N_VGetArrayPointer(z); 587cb94ee6SHong Zhang ierr = VecPlaceArray(rr,r_data);CHKERRQ(ierr); 597cb94ee6SHong Zhang ierr = VecPlaceArray(zz,z_data);CHKERRQ(ierr); 604ac7836bSHong Zhang 617cb94ee6SHong Zhang /* Solve the Px=r and put the result in zz */ 62dcbc6d53SSean Farley ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr); 637cb94ee6SHong Zhang ierr = PCApply(pc,rr,zz);CHKERRQ(ierr); 647cb94ee6SHong Zhang ierr = VecResetArray(rr);CHKERRQ(ierr); 657cb94ee6SHong Zhang ierr = VecResetArray(zz);CHKERRQ(ierr); 667cb94ee6SHong Zhang PetscFunctionReturn(0); 677cb94ee6SHong Zhang } 687cb94ee6SHong Zhang 697cb94ee6SHong Zhang /* 707cb94ee6SHong Zhang TSFunction_Sundials - routine that we provide to Sundials that applies the right hand side. 717cb94ee6SHong Zhang */ 724ac7836bSHong Zhang int TSFunction_Sundials(realtype t,N_Vector y,N_Vector ydot,void *ctx) 737cb94ee6SHong Zhang { 747cb94ee6SHong Zhang TS ts = (TS) ctx; 755fcef5e4SJed Brown DM dm; 76942e3340SBarry Smith DMTS tsdm; 775fcef5e4SJed Brown TSIFunction ifunction; 78ce94432eSBarry Smith MPI_Comm comm; 797cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 800679a0aeSJed Brown Vec yy = cvode->w1,yyd = cvode->w2,yydot = cvode->ydot; 817cb94ee6SHong Zhang PetscScalar *y_data,*ydot_data; 827cb94ee6SHong Zhang PetscErrorCode ierr; 837cb94ee6SHong Zhang 847cb94ee6SHong Zhang PetscFunctionBegin; 85ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 865ff03cf7SDinesh Kaushik /* Make the PETSc work vectors yy and yyd point to the arrays in the SUNDIALS vectors y and ydot respectively*/ 877cb94ee6SHong Zhang y_data = (PetscScalar*) N_VGetArrayPointer(y); 887cb94ee6SHong Zhang ydot_data = (PetscScalar*) N_VGetArrayPointer(ydot); 894ac7836bSHong Zhang ierr = VecPlaceArray(yy,y_data);CHKERRABORT(comm,ierr); 904ac7836bSHong Zhang ierr = VecPlaceArray(yyd,ydot_data);CHKERRABORT(comm,ierr); 914ac7836bSHong Zhang 925fcef5e4SJed Brown /* Now compute the right hand side function, via IFunction unless only the more efficient RHSFunction is set */ 935fcef5e4SJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 94942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 950298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 965fcef5e4SJed Brown if (!ifunction) { 97bc0cc02bSJed Brown ierr = TSComputeRHSFunction(ts,t,yy,yyd);CHKERRQ(ierr); 98bc0cc02bSJed Brown } else { /* If rhsfunction is also set, this computes both parts and shifts them to the right */ 99bc0cc02bSJed Brown ierr = VecZeroEntries(yydot);CHKERRQ(ierr); 1000679a0aeSJed Brown ierr = TSComputeIFunction(ts,t,yy,yydot,yyd,PETSC_FALSE);CHKERRABORT(comm,ierr); 1010679a0aeSJed Brown ierr = VecScale(yyd,-1.);CHKERRQ(ierr); 102bc0cc02bSJed Brown } 1037cb94ee6SHong Zhang ierr = VecResetArray(yy);CHKERRABORT(comm,ierr); 1047cb94ee6SHong Zhang ierr = VecResetArray(yyd);CHKERRABORT(comm,ierr); 1054ac7836bSHong Zhang PetscFunctionReturn(0); 1067cb94ee6SHong Zhang } 1077cb94ee6SHong Zhang 1087cb94ee6SHong Zhang /* 109b4eba00bSSean Farley TSStep_Sundials - Calls Sundials to integrate the ODE. 1107cb94ee6SHong Zhang */ 111b4eba00bSSean Farley PetscErrorCode TSStep_Sundials(TS ts) 1127cb94ee6SHong Zhang { 1137cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 1147cb94ee6SHong Zhang PetscErrorCode ierr; 115b4eba00bSSean Farley PetscInt flag; 116be5899b3SLisandro Dalcin long int nits,lits,nsteps; 1177cb94ee6SHong Zhang realtype t,tout; 1187cb94ee6SHong Zhang PetscScalar *y_data; 1197cb94ee6SHong Zhang void *mem; 1207cb94ee6SHong Zhang 1217cb94ee6SHong Zhang PetscFunctionBegin; 12216016685SHong Zhang mem = cvode->mem; 1237cb94ee6SHong Zhang tout = ts->max_time; 1247cb94ee6SHong Zhang ierr = VecGetArray(ts->vec_sol,&y_data);CHKERRQ(ierr); 1257cb94ee6SHong Zhang N_VSetArrayPointer((realtype*)y_data,cvode->y); 1260298fd71SBarry Smith ierr = VecRestoreArray(ts->vec_sol,NULL);CHKERRQ(ierr); 127186e87acSLisandro Dalcin 1289687d888SLisandro Dalcin /* We would like to TSPreStage() and TSPostStage() 1299687d888SLisandro Dalcin * before each stage solve but CVode does not appear to support this. */ 130be5899b3SLisandro Dalcin if (cvode->monitorstep) 131be5899b3SLisandro Dalcin flag = CVode(mem,tout,cvode->y,&t,CV_ONE_STEP); 132be5899b3SLisandro Dalcin else 133be5899b3SLisandro Dalcin flag = CVode(mem,tout,cvode->y,&t,CV_NORMAL); 1349f94935aSHong Zhang 1359f94935aSHong Zhang if (flag) { /* display error message */ 1369f94935aSHong Zhang switch (flag) { 1379f94935aSHong Zhang case CV_ILL_INPUT: 1389f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_ILL_INPUT"); 1399f94935aSHong Zhang break; 1409f94935aSHong Zhang case CV_TOO_CLOSE: 1419f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_TOO_CLOSE"); 1429f94935aSHong Zhang break; 1433c7fefeeSJed Brown case CV_TOO_MUCH_WORK: { 1449f94935aSHong Zhang PetscReal tcur; 1459f94935aSHong Zhang ierr = CVodeGetNumSteps(mem,&nsteps);CHKERRQ(ierr); 1469f94935aSHong Zhang ierr = CVodeGetCurrentTime(mem,&tcur);CHKERRQ(ierr); 14719eac22cSLisandro Dalcin SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_TOO_MUCH_WORK. At t=%g, nsteps %D exceeds maxstep %D. Increase '-ts_max_steps <>' or modify TSSetMaxSteps()",(double)tcur,nsteps,ts->max_steps); 1483c7fefeeSJed Brown } break; 1499f94935aSHong Zhang case CV_TOO_MUCH_ACC: 1509f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_TOO_MUCH_ACC"); 1519f94935aSHong Zhang break; 1529f94935aSHong Zhang case CV_ERR_FAILURE: 1539f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_ERR_FAILURE"); 1549f94935aSHong Zhang break; 1559f94935aSHong Zhang case CV_CONV_FAILURE: 1569f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_CONV_FAILURE"); 1579f94935aSHong Zhang break; 1589f94935aSHong Zhang case CV_LINIT_FAIL: 1599f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_LINIT_FAIL"); 1609f94935aSHong Zhang break; 1619f94935aSHong Zhang case CV_LSETUP_FAIL: 1629f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_LSETUP_FAIL"); 1639f94935aSHong Zhang break; 1649f94935aSHong Zhang case CV_LSOLVE_FAIL: 1659f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_LSOLVE_FAIL"); 1669f94935aSHong Zhang break; 1679f94935aSHong Zhang case CV_RHSFUNC_FAIL: 1689f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_RHSFUNC_FAIL"); 1699f94935aSHong Zhang break; 1709f94935aSHong Zhang case CV_FIRST_RHSFUNC_ERR: 1719f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_FIRST_RHSFUNC_ERR"); 1729f94935aSHong Zhang break; 1739f94935aSHong Zhang case CV_REPTD_RHSFUNC_ERR: 1749f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_REPTD_RHSFUNC_ERR"); 1759f94935aSHong Zhang break; 1769f94935aSHong Zhang case CV_UNREC_RHSFUNC_ERR: 1779f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_UNREC_RHSFUNC_ERR"); 1789f94935aSHong Zhang break; 1799f94935aSHong Zhang case CV_RTFUNC_FAIL: 1809f94935aSHong Zhang SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, CV_RTFUNC_FAIL"); 1819f94935aSHong Zhang break; 1829f94935aSHong Zhang default: 1839f94935aSHong Zhang SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVode() fails, flag %d",flag); 1849f94935aSHong Zhang } 1859f94935aSHong Zhang } 1869f94935aSHong Zhang 187be5899b3SLisandro Dalcin /* log inner nonlinear and linear iterations */ 188be5899b3SLisandro Dalcin ierr = CVodeGetNumNonlinSolvIters(mem,&nits);CHKERRQ(ierr); 189be5899b3SLisandro Dalcin ierr = CVSpilsGetNumLinIters(mem,&lits);CHKERRQ(ierr); 190be5899b3SLisandro Dalcin ts->snes_its += nits; ts->ksp_its = lits; 191be5899b3SLisandro Dalcin 1927cb94ee6SHong Zhang /* copy the solution from cvode->y to cvode->update and sol */ 1937cb94ee6SHong Zhang ierr = VecPlaceArray(cvode->w1,y_data);CHKERRQ(ierr); 1947cb94ee6SHong Zhang ierr = VecCopy(cvode->w1,cvode->update);CHKERRQ(ierr); 1957cb94ee6SHong Zhang ierr = VecResetArray(cvode->w1);CHKERRQ(ierr); 196bc0cc02bSJed Brown ierr = VecCopy(cvode->update,ts->vec_sol);CHKERRQ(ierr); 197186e87acSLisandro Dalcin 198186e87acSLisandro Dalcin ts->time_step = t - ts->ptime; 199186e87acSLisandro Dalcin ts->ptime = t; 200186e87acSLisandro Dalcin 2019f94935aSHong Zhang ierr = CVodeGetNumSteps(mem,&nsteps);CHKERRQ(ierr); 2022808aa04SLisandro Dalcin if (!cvode->monitorstep) ts->steps += nsteps - 1; /* TSStep() increments the step counter by one */ 203b4eba00bSSean Farley PetscFunctionReturn(0); 204b4eba00bSSean Farley } 205b4eba00bSSean Farley 206b4eba00bSSean Farley static PetscErrorCode TSInterpolate_Sundials(TS ts,PetscReal t,Vec X) 207b4eba00bSSean Farley { 208b4eba00bSSean Farley TS_Sundials *cvode = (TS_Sundials*)ts->data; 209b4eba00bSSean Farley N_Vector y; 210b4eba00bSSean Farley PetscErrorCode ierr; 211b4eba00bSSean Farley PetscScalar *x_data; 212b4eba00bSSean Farley PetscInt glosize,locsize; 213b4eba00bSSean Farley 214b4eba00bSSean Farley PetscFunctionBegin; 215b4eba00bSSean Farley /* get the vector size */ 216b4eba00bSSean Farley ierr = VecGetSize(X,&glosize);CHKERRQ(ierr); 217b4eba00bSSean Farley ierr = VecGetLocalSize(X,&locsize);CHKERRQ(ierr); 2184d78c9e0SClaas Abert ierr = VecGetArray(X,&x_data);CHKERRQ(ierr); 219b4eba00bSSean Farley 2204d78c9e0SClaas Abert /* Initialize N_Vec y with x_data */ 2214d78c9e0SClaas Abert y = N_VMake_Parallel(cvode->comm_sundials,locsize,glosize,(realtype*)x_data); 222691b26d3SBarry Smith if (!y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Interpolated y is not allocated"); 223b4eba00bSSean Farley 224b4eba00bSSean Farley ierr = CVodeGetDky(cvode->mem,t,0,y);CHKERRQ(ierr); 225b4eba00bSSean Farley ierr = VecRestoreArray(X,&x_data);CHKERRQ(ierr); 2267cb94ee6SHong Zhang PetscFunctionReturn(0); 2277cb94ee6SHong Zhang } 2287cb94ee6SHong Zhang 229277b19d0SLisandro Dalcin PetscErrorCode TSReset_Sundials(TS ts) 230277b19d0SLisandro Dalcin { 231277b19d0SLisandro Dalcin TS_Sundials *cvode = (TS_Sundials*)ts->data; 232277b19d0SLisandro Dalcin PetscErrorCode ierr; 233277b19d0SLisandro Dalcin 234277b19d0SLisandro Dalcin PetscFunctionBegin; 2355c6b4a3dSJed Brown ierr = VecDestroy(&cvode->update);CHKERRQ(ierr); 2360679a0aeSJed Brown ierr = VecDestroy(&cvode->ydot);CHKERRQ(ierr); 2375c6b4a3dSJed Brown ierr = VecDestroy(&cvode->w1);CHKERRQ(ierr); 2385c6b4a3dSJed Brown ierr = VecDestroy(&cvode->w2);CHKERRQ(ierr); 239bbd56ea5SKarl Rupp if (cvode->mem) CVodeFree(&cvode->mem); 240277b19d0SLisandro Dalcin PetscFunctionReturn(0); 241277b19d0SLisandro Dalcin } 242277b19d0SLisandro Dalcin 2437cb94ee6SHong Zhang PetscErrorCode TSDestroy_Sundials(TS ts) 2447cb94ee6SHong Zhang { 2457cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 2467cb94ee6SHong Zhang PetscErrorCode ierr; 2477cb94ee6SHong Zhang 2487cb94ee6SHong Zhang PetscFunctionBegin; 249277b19d0SLisandro Dalcin ierr = TSReset_Sundials(ts);CHKERRQ(ierr); 250ffc4695bSBarry Smith ierr = MPI_Comm_free(&(cvode->comm_sundials));CHKERRMPI(ierr); 251277b19d0SLisandro Dalcin ierr = PetscFree(ts->data);CHKERRQ(ierr); 252bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetType_C",NULL);CHKERRQ(ierr); 253bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxl_C",NULL);CHKERRQ(ierr); 254bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetLinearTolerance_C",NULL);CHKERRQ(ierr); 255bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetGramSchmidtType_C",NULL);CHKERRQ(ierr); 256bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetTolerance_C",NULL);CHKERRQ(ierr); 257bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMinTimeStep_C",NULL);CHKERRQ(ierr); 258bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxTimeStep_C",NULL);CHKERRQ(ierr); 259bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetPC_C",NULL);CHKERRQ(ierr); 260bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetIterations_C",NULL);CHKERRQ(ierr); 261bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsMonitorInternalSteps_C",NULL);CHKERRQ(ierr); 2627cb94ee6SHong Zhang PetscFunctionReturn(0); 2637cb94ee6SHong Zhang } 2647cb94ee6SHong Zhang 265214bc6a2SJed Brown PetscErrorCode TSSetUp_Sundials(TS ts) 2667cb94ee6SHong Zhang { 2677cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 2687cb94ee6SHong Zhang PetscErrorCode ierr; 26916016685SHong Zhang PetscInt glosize,locsize,i,flag; 2707cb94ee6SHong Zhang PetscScalar *y_data,*parray; 27116016685SHong Zhang void *mem; 272dcbc6d53SSean Farley PC pc; 27319fd82e9SBarry Smith PCType pctype; 274ace3abfcSBarry Smith PetscBool pcnone; 2757cb94ee6SHong Zhang 2767cb94ee6SHong Zhang PetscFunctionBegin; 277236c45dcSShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for exact final time option 'MATCHSTEP' when using Sundials"); 278236c45dcSShri Abhyankar 2797cb94ee6SHong Zhang /* get the vector size */ 2807cb94ee6SHong Zhang ierr = VecGetSize(ts->vec_sol,&glosize);CHKERRQ(ierr); 2817cb94ee6SHong Zhang ierr = VecGetLocalSize(ts->vec_sol,&locsize);CHKERRQ(ierr); 2827cb94ee6SHong Zhang 2837cb94ee6SHong Zhang /* allocate the memory for N_Vec y */ 284a07356b8SSatish Balay cvode->y = N_VNew_Parallel(cvode->comm_sundials,locsize,glosize); 285691b26d3SBarry Smith if (!cvode->y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cvode->y is not allocated"); 2867cb94ee6SHong Zhang 28728adb3f7SHong Zhang /* initialize N_Vec y: copy ts->vec_sol to cvode->y */ 2887cb94ee6SHong Zhang ierr = VecGetArray(ts->vec_sol,&parray);CHKERRQ(ierr); 2897cb94ee6SHong Zhang y_data = (PetscScalar*) N_VGetArrayPointer(cvode->y); 2907cb94ee6SHong Zhang for (i = 0; i < locsize; i++) y_data[i] = parray[i]; 2910298fd71SBarry Smith ierr = VecRestoreArray(ts->vec_sol,NULL);CHKERRQ(ierr); 29242528757SHong Zhang 2937cb94ee6SHong Zhang ierr = VecDuplicate(ts->vec_sol,&cvode->update);CHKERRQ(ierr); 2940679a0aeSJed Brown ierr = VecDuplicate(ts->vec_sol,&cvode->ydot);CHKERRQ(ierr); 2953bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)cvode->update);CHKERRQ(ierr); 2963bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)cvode->ydot);CHKERRQ(ierr); 2977cb94ee6SHong Zhang 2987cb94ee6SHong Zhang /* 2997cb94ee6SHong Zhang Create work vectors for the TSPSolve_Sundials() routine. Note these are 3007cb94ee6SHong Zhang allocated with zero space arrays because the actual array space is provided 3017cb94ee6SHong Zhang by Sundials and set using VecPlaceArray(). 3027cb94ee6SHong Zhang */ 303c793f718SLisandro Dalcin ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)ts),1,locsize,PETSC_DECIDE,NULL,&cvode->w1);CHKERRQ(ierr); 304c793f718SLisandro Dalcin ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)ts),1,locsize,PETSC_DECIDE,NULL,&cvode->w2);CHKERRQ(ierr); 3053bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)cvode->w1);CHKERRQ(ierr); 3063bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)cvode->w2);CHKERRQ(ierr); 30716016685SHong Zhang 30816016685SHong Zhang /* Call CVodeCreate to create the solver memory and the use of a Newton iteration */ 30916016685SHong Zhang mem = CVodeCreate(cvode->cvode_type, CV_NEWTON); 310e32f2f54SBarry Smith if (!mem) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"CVodeCreate() fails"); 31116016685SHong Zhang cvode->mem = mem; 31216016685SHong Zhang 31316016685SHong Zhang /* Set the pointer to user-defined data */ 31416016685SHong Zhang flag = CVodeSetUserData(mem, ts); 315e32f2f54SBarry Smith if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVodeSetUserData() fails"); 31616016685SHong Zhang 317fc6b9e64SJed Brown /* Sundials may choose to use a smaller initial step, but will never use a larger step. */ 318cdbf8f93SLisandro Dalcin flag = CVodeSetInitStep(mem,(realtype)ts->time_step); 319ce94432eSBarry Smith if (flag) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_LIB,"CVodeSetInitStep() failed"); 320f1cd61daSJed Brown if (cvode->mindt > 0) { 321f1cd61daSJed Brown flag = CVodeSetMinStep(mem,(realtype)cvode->mindt); 3229f94935aSHong Zhang if (flag) { 323ce94432eSBarry Smith if (flag == CV_MEM_NULL) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_LIB,"CVodeSetMinStep() failed, cvode_mem pointer is NULL"); 324ce94432eSBarry Smith else if (flag == CV_ILL_INPUT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_LIB,"CVodeSetMinStep() failed, hmin is nonpositive or it exceeds the maximum allowable step size"); 325ce94432eSBarry Smith else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_LIB,"CVodeSetMinStep() failed"); 3269f94935aSHong Zhang } 327f1cd61daSJed Brown } 328f1cd61daSJed Brown if (cvode->maxdt > 0) { 329f1cd61daSJed Brown flag = CVodeSetMaxStep(mem,(realtype)cvode->maxdt); 330ce94432eSBarry Smith if (flag) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_LIB,"CVodeSetMaxStep() failed"); 331f1cd61daSJed Brown } 332f1cd61daSJed Brown 33316016685SHong Zhang /* Call CVodeInit to initialize the integrator memory and specify the 334a5b23f4aSJose E. Roman * user's right hand side function in u'=f(t,u), the initial time T0, and 33516016685SHong Zhang * the initial dependent variable vector cvode->y */ 33616016685SHong Zhang flag = CVodeInit(mem,TSFunction_Sundials,ts->ptime,cvode->y); 337bbd56ea5SKarl Rupp if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVodeInit() fails, flag %d",flag); 33816016685SHong Zhang 3399f94935aSHong Zhang /* specifies scalar relative and absolute tolerances */ 34016016685SHong Zhang flag = CVodeSStolerances(mem,cvode->reltol,cvode->abstol); 341bbd56ea5SKarl Rupp if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVodeSStolerances() fails, flag %d",flag); 34216016685SHong Zhang 343c4e80e11SFlorian /* Specify max order of BDF / ADAMS method */ 344c4e80e11SFlorian if (cvode->maxord != PETSC_DEFAULT) { 345c4e80e11SFlorian flag = CVodeSetMaxOrd(mem,cvode->maxord); 346c4e80e11SFlorian if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVodeSetMaxOrd() fails, flag %d",flag); 347c4e80e11SFlorian } 348c4e80e11SFlorian 3499f94935aSHong Zhang /* Specify max num of steps to be taken by cvode in its attempt to reach the next output time */ 3509f94935aSHong Zhang flag = CVodeSetMaxNumSteps(mem,ts->max_steps); 351be5899b3SLisandro Dalcin if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVodeSetMaxNumSteps() fails, flag %d",flag); 35216016685SHong Zhang 35316016685SHong Zhang /* call CVSpgmr to use GMRES as the linear solver. */ 35416016685SHong Zhang /* setup the ode integrator with the given preconditioner */ 355dcbc6d53SSean Farley ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr); 356dcbc6d53SSean Farley ierr = PCGetType(pc,&pctype);CHKERRQ(ierr); 357251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCNONE,&pcnone);CHKERRQ(ierr); 35816016685SHong Zhang if (pcnone) { 35916016685SHong Zhang flag = CVSpgmr(mem,PREC_NONE,0); 360e32f2f54SBarry Smith if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVSpgmr() fails, flag %d",flag); 36116016685SHong Zhang } else { 362f61b2b6aSHong Zhang flag = CVSpgmr(mem,PREC_LEFT,cvode->maxl); 363e32f2f54SBarry Smith if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVSpgmr() fails, flag %d",flag); 36416016685SHong Zhang 36516016685SHong Zhang /* Set preconditioner and solve routines Precond and PSolve, 36616016685SHong Zhang and the pointer to the user-defined block data */ 36716016685SHong Zhang flag = CVSpilsSetPreconditioner(mem,TSPrecond_Sundials,TSPSolve_Sundials); 368e32f2f54SBarry Smith if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVSpilsSetPreconditioner() fails, flag %d", flag); 36916016685SHong Zhang } 37016016685SHong Zhang 37116016685SHong Zhang flag = CVSpilsSetGSType(mem, MODIFIED_GS); 372e32f2f54SBarry Smith if (flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CVSpgmrSetGSType() fails, flag %d",flag); 3737cb94ee6SHong Zhang PetscFunctionReturn(0); 3747cb94ee6SHong Zhang } 3757cb94ee6SHong Zhang 3766fadb2cdSHong Zhang /* type of CVODE linear multistep method */ 377c793f718SLisandro Dalcin const char *const TSSundialsLmmTypes[] = {"","ADAMS","BDF","TSSundialsLmmType","SUNDIALS_",NULL}; 3786fadb2cdSHong Zhang /* type of G-S orthogonalization used by CVODE linear solver */ 379c793f718SLisandro Dalcin const char *const TSSundialsGramSchmidtTypes[] = {"","MODIFIED","CLASSICAL","TSSundialsGramSchmidtType","SUNDIALS_",NULL}; 380a04cf4d8SBarry Smith 3814416b707SBarry Smith PetscErrorCode TSSetFromOptions_Sundials(PetscOptionItems *PetscOptionsObject,TS ts) 3827cb94ee6SHong Zhang { 3837cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 3847cb94ee6SHong Zhang PetscErrorCode ierr; 3857cb94ee6SHong Zhang int indx; 386ace3abfcSBarry Smith PetscBool flag; 3877bda3a07SJed Brown PC pc; 3887cb94ee6SHong Zhang 3897cb94ee6SHong Zhang PetscFunctionBegin; 390e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SUNDIALS ODE solver options");CHKERRQ(ierr); 3916fadb2cdSHong Zhang ierr = PetscOptionsEList("-ts_sundials_type","Scheme","TSSundialsSetType",TSSundialsLmmTypes,3,TSSundialsLmmTypes[cvode->cvode_type],&indx,&flag);CHKERRQ(ierr); 3927cb94ee6SHong Zhang if (flag) { 3936fadb2cdSHong Zhang ierr = TSSundialsSetType(ts,(TSSundialsLmmType)indx);CHKERRQ(ierr); 3947cb94ee6SHong Zhang } 3956fadb2cdSHong Zhang ierr = PetscOptionsEList("-ts_sundials_gramschmidt_type","Type of orthogonalization","TSSundialsSetGramSchmidtType",TSSundialsGramSchmidtTypes,3,TSSundialsGramSchmidtTypes[cvode->gtype],&indx,&flag);CHKERRQ(ierr); 3967cb94ee6SHong Zhang if (flag) { 3977cb94ee6SHong Zhang ierr = TSSundialsSetGramSchmidtType(ts,(TSSundialsGramSchmidtType)indx);CHKERRQ(ierr); 3987cb94ee6SHong Zhang } 3990298fd71SBarry Smith ierr = PetscOptionsReal("-ts_sundials_atol","Absolute tolerance for convergence","TSSundialsSetTolerance",cvode->abstol,&cvode->abstol,NULL);CHKERRQ(ierr); 4000298fd71SBarry Smith ierr = PetscOptionsReal("-ts_sundials_rtol","Relative tolerance for convergence","TSSundialsSetTolerance",cvode->reltol,&cvode->reltol,NULL);CHKERRQ(ierr); 4010298fd71SBarry Smith ierr = PetscOptionsReal("-ts_sundials_mindt","Minimum step size","TSSundialsSetMinTimeStep",cvode->mindt,&cvode->mindt,NULL);CHKERRQ(ierr); 4020298fd71SBarry Smith ierr = PetscOptionsReal("-ts_sundials_maxdt","Maximum step size","TSSundialsSetMaxTimeStep",cvode->maxdt,&cvode->maxdt,NULL);CHKERRQ(ierr); 40394ae4db5SBarry Smith ierr = PetscOptionsReal("-ts_sundials_linear_tolerance","Convergence tolerance for linear solve","TSSundialsSetLinearTolerance",cvode->linear_tol,&cvode->linear_tol,NULL);CHKERRQ(ierr); 404c4e80e11SFlorian ierr = PetscOptionsInt("-ts_sundials_maxord","Max Order for BDF/Adams method","TSSundialsSetMaxOrd",cvode->maxord,&cvode->maxord,NULL);CHKERRQ(ierr); 40594ae4db5SBarry Smith ierr = PetscOptionsInt("-ts_sundials_maxl","Max dimension of the Krylov subspace","TSSundialsSetMaxl",cvode->maxl,&cvode->maxl,NULL);CHKERRQ(ierr); 406be5899b3SLisandro Dalcin ierr = PetscOptionsBool("-ts_sundials_monitor_steps","Monitor SUNDIALS internal steps","TSSundialsMonitorInternalSteps",cvode->monitorstep,&cvode->monitorstep,NULL);CHKERRQ(ierr); 4077cb94ee6SHong Zhang ierr = PetscOptionsTail();CHKERRQ(ierr); 4087bda3a07SJed Brown ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr); 4097bda3a07SJed Brown ierr = PCSetFromOptions(pc);CHKERRQ(ierr); 4107cb94ee6SHong Zhang PetscFunctionReturn(0); 4117cb94ee6SHong Zhang } 4127cb94ee6SHong Zhang 4137cb94ee6SHong Zhang PetscErrorCode TSView_Sundials(TS ts,PetscViewer viewer) 4147cb94ee6SHong Zhang { 4157cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 4167cb94ee6SHong Zhang PetscErrorCode ierr; 4177cb94ee6SHong Zhang char *type; 4187cb94ee6SHong Zhang char atype[] = "Adams"; 4197cb94ee6SHong Zhang char btype[] = "BDF: backward differentiation formula"; 420ace3abfcSBarry Smith PetscBool iascii,isstring; 4212c823083SHong Zhang long int nsteps,its,nfevals,nlinsetups,nfails,itmp; 4222c823083SHong Zhang PetscInt qlast,qcur; 4235d47aee6SHong Zhang PetscReal hinused,hlast,hcur,tcur,tolsfac; 42442528757SHong Zhang PC pc; 4257cb94ee6SHong Zhang 4267cb94ee6SHong Zhang PetscFunctionBegin; 427bbd56ea5SKarl Rupp if (cvode->cvode_type == SUNDIALS_ADAMS) type = atype; 428bbd56ea5SKarl Rupp else type = btype; 4297cb94ee6SHong Zhang 430251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 431251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 4327cb94ee6SHong Zhang if (iascii) { 4337cb94ee6SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials integrater does not use SNES!\n");CHKERRQ(ierr); 4347cb94ee6SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials integrater type %s\n",type);CHKERRQ(ierr); 435c4e80e11SFlorian ierr = PetscViewerASCIIPrintf(viewer,"Sundials maxord %D\n",cvode->maxord);CHKERRQ(ierr); 436c4e80e11SFlorian ierr = PetscViewerASCIIPrintf(viewer,"Sundials abs tol %g rel tol %g\n",(double)cvode->abstol,(double)cvode->reltol);CHKERRQ(ierr); 437c4e80e11SFlorian ierr = PetscViewerASCIIPrintf(viewer,"Sundials linear solver tolerance factor %g\n",(double)cvode->linear_tol);CHKERRQ(ierr); 438f61b2b6aSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials max dimension of Krylov subspace %D\n",cvode->maxl);CHKERRQ(ierr); 4397cb94ee6SHong Zhang if (cvode->gtype == SUNDIALS_MODIFIED_GS) { 4407cb94ee6SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials using modified Gram-Schmidt for orthogonalization in GMRES\n");CHKERRQ(ierr); 4417cb94ee6SHong Zhang } else { 4427cb94ee6SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials using unmodified (classical) Gram-Schmidt for orthogonalization in GMRES\n");CHKERRQ(ierr); 4437cb94ee6SHong Zhang } 444c4e80e11SFlorian if (cvode->mindt > 0) {ierr = PetscViewerASCIIPrintf(viewer,"Sundials minimum time step %g\n",(double)cvode->mindt);CHKERRQ(ierr);} 445c4e80e11SFlorian if (cvode->maxdt > 0) {ierr = PetscViewerASCIIPrintf(viewer,"Sundials maximum time step %g\n",(double)cvode->maxdt);CHKERRQ(ierr);} 4462c823083SHong Zhang 4475d47aee6SHong Zhang /* Outputs from CVODE, CVSPILS */ 4485d47aee6SHong Zhang ierr = CVodeGetTolScaleFactor(cvode->mem,&tolsfac);CHKERRQ(ierr); 4495d47aee6SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials suggested factor for tolerance scaling %g\n",tolsfac);CHKERRQ(ierr); 4502c823083SHong Zhang ierr = CVodeGetIntegratorStats(cvode->mem,&nsteps,&nfevals, 4512c823083SHong Zhang &nlinsetups,&nfails,&qlast,&qcur, 4522c823083SHong Zhang &hinused,&hlast,&hcur,&tcur);CHKERRQ(ierr); 4532c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials cumulative number of internal steps %D\n",nsteps);CHKERRQ(ierr); 4542c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of calls to rhs function %D\n",nfevals);CHKERRQ(ierr); 4552c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of calls to linear solver setup function %D\n",nlinsetups);CHKERRQ(ierr); 4562c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of error test failures %D\n",nfails);CHKERRQ(ierr); 4572c823083SHong Zhang 4582c823083SHong Zhang ierr = CVodeGetNonlinSolvStats(cvode->mem,&its,&nfails);CHKERRQ(ierr); 4592c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of nonlinear solver iterations %D\n",its);CHKERRQ(ierr); 4602c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of nonlinear convergence failure %D\n",nfails);CHKERRQ(ierr); 4612c823083SHong Zhang 4622c823083SHong Zhang ierr = CVSpilsGetNumLinIters(cvode->mem, &its);CHKERRQ(ierr); /* its = no. of calls to TSPrecond_Sundials() */ 4632c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of linear iterations %D\n",its);CHKERRQ(ierr); 4642c823083SHong Zhang ierr = CVSpilsGetNumConvFails(cvode->mem,&itmp);CHKERRQ(ierr); 4652c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of linear convergence failures %D\n",itmp);CHKERRQ(ierr); 46642528757SHong Zhang 46742528757SHong Zhang ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr); 46842528757SHong Zhang ierr = PCView(pc,viewer);CHKERRQ(ierr); 4692c823083SHong Zhang ierr = CVSpilsGetNumPrecEvals(cvode->mem,&itmp);CHKERRQ(ierr); 4702c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of preconditioner evaluations %D\n",itmp);CHKERRQ(ierr); 4712c823083SHong Zhang ierr = CVSpilsGetNumPrecSolves(cvode->mem,&itmp);CHKERRQ(ierr); 4722c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of preconditioner solves %D\n",itmp);CHKERRQ(ierr); 47342528757SHong Zhang 4742c823083SHong Zhang ierr = CVSpilsGetNumJtimesEvals(cvode->mem,&itmp);CHKERRQ(ierr); 4752c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of Jacobian-vector product evaluations %D\n",itmp);CHKERRQ(ierr); 4762c823083SHong Zhang ierr = CVSpilsGetNumRhsEvals(cvode->mem,&itmp);CHKERRQ(ierr); 4772c823083SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"Sundials no. of rhs calls for finite diff. Jacobian-vector evals %D\n",itmp);CHKERRQ(ierr); 4787cb94ee6SHong Zhang } else if (isstring) { 4797cb94ee6SHong Zhang ierr = PetscViewerStringSPrintf(viewer,"Sundials type %s",type);CHKERRQ(ierr); 4807cb94ee6SHong Zhang } 4817cb94ee6SHong Zhang PetscFunctionReturn(0); 4827cb94ee6SHong Zhang } 4837cb94ee6SHong Zhang 4847cb94ee6SHong Zhang /* --------------------------------------------------------------------------*/ 4857087cfbeSBarry Smith PetscErrorCode TSSundialsSetType_Sundials(TS ts,TSSundialsLmmType type) 4867cb94ee6SHong Zhang { 4877cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 4887cb94ee6SHong Zhang 4897cb94ee6SHong Zhang PetscFunctionBegin; 4907cb94ee6SHong Zhang cvode->cvode_type = type; 4917cb94ee6SHong Zhang PetscFunctionReturn(0); 4927cb94ee6SHong Zhang } 4937cb94ee6SHong Zhang 494f61b2b6aSHong Zhang PetscErrorCode TSSundialsSetMaxl_Sundials(TS ts,PetscInt maxl) 4957cb94ee6SHong Zhang { 4967cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 4977cb94ee6SHong Zhang 4987cb94ee6SHong Zhang PetscFunctionBegin; 499f61b2b6aSHong Zhang cvode->maxl = maxl; 5007cb94ee6SHong Zhang PetscFunctionReturn(0); 5017cb94ee6SHong Zhang } 5027cb94ee6SHong Zhang 5037087cfbeSBarry Smith PetscErrorCode TSSundialsSetLinearTolerance_Sundials(TS ts,double tol) 5047cb94ee6SHong Zhang { 5057cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 5067cb94ee6SHong Zhang 5077cb94ee6SHong Zhang PetscFunctionBegin; 5087cb94ee6SHong Zhang cvode->linear_tol = tol; 5097cb94ee6SHong Zhang PetscFunctionReturn(0); 5107cb94ee6SHong Zhang } 5117cb94ee6SHong Zhang 5127087cfbeSBarry Smith PetscErrorCode TSSundialsSetGramSchmidtType_Sundials(TS ts,TSSundialsGramSchmidtType type) 5137cb94ee6SHong Zhang { 5147cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 5157cb94ee6SHong Zhang 5167cb94ee6SHong Zhang PetscFunctionBegin; 5177cb94ee6SHong Zhang cvode->gtype = type; 5187cb94ee6SHong Zhang PetscFunctionReturn(0); 5197cb94ee6SHong Zhang } 5207cb94ee6SHong Zhang 5217087cfbeSBarry Smith PetscErrorCode TSSundialsSetTolerance_Sundials(TS ts,double aabs,double rel) 5227cb94ee6SHong Zhang { 5237cb94ee6SHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 5247cb94ee6SHong Zhang 5257cb94ee6SHong Zhang PetscFunctionBegin; 5267cb94ee6SHong Zhang if (aabs != PETSC_DECIDE) cvode->abstol = aabs; 5277cb94ee6SHong Zhang if (rel != PETSC_DECIDE) cvode->reltol = rel; 5287cb94ee6SHong Zhang PetscFunctionReturn(0); 5297cb94ee6SHong Zhang } 5307cb94ee6SHong Zhang 5317087cfbeSBarry Smith PetscErrorCode TSSundialsSetMinTimeStep_Sundials(TS ts,PetscReal mindt) 532f1cd61daSJed Brown { 533f1cd61daSJed Brown TS_Sundials *cvode = (TS_Sundials*)ts->data; 534f1cd61daSJed Brown 535f1cd61daSJed Brown PetscFunctionBegin; 536f1cd61daSJed Brown cvode->mindt = mindt; 537f1cd61daSJed Brown PetscFunctionReturn(0); 538f1cd61daSJed Brown } 539f1cd61daSJed Brown 5407087cfbeSBarry Smith PetscErrorCode TSSundialsSetMaxTimeStep_Sundials(TS ts,PetscReal maxdt) 541f1cd61daSJed Brown { 542f1cd61daSJed Brown TS_Sundials *cvode = (TS_Sundials*)ts->data; 543f1cd61daSJed Brown 544f1cd61daSJed Brown PetscFunctionBegin; 545f1cd61daSJed Brown cvode->maxdt = maxdt; 546f1cd61daSJed Brown PetscFunctionReturn(0); 547f1cd61daSJed Brown } 5487087cfbeSBarry Smith PetscErrorCode TSSundialsGetPC_Sundials(TS ts,PC *pc) 5497cb94ee6SHong Zhang { 550dcbc6d53SSean Farley SNES snes; 551dcbc6d53SSean Farley KSP ksp; 552dcbc6d53SSean Farley PetscErrorCode ierr; 5537cb94ee6SHong Zhang 5547cb94ee6SHong Zhang PetscFunctionBegin; 555dcbc6d53SSean Farley ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 556dcbc6d53SSean Farley ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 557dcbc6d53SSean Farley ierr = KSPGetPC(ksp,pc);CHKERRQ(ierr); 5587cb94ee6SHong Zhang PetscFunctionReturn(0); 5597cb94ee6SHong Zhang } 5607cb94ee6SHong Zhang 5617087cfbeSBarry Smith PetscErrorCode TSSundialsGetIterations_Sundials(TS ts,int *nonlin,int *lin) 5627cb94ee6SHong Zhang { 5637cb94ee6SHong Zhang PetscFunctionBegin; 5645ef26d82SJed Brown if (nonlin) *nonlin = ts->snes_its; 5655ef26d82SJed Brown if (lin) *lin = ts->ksp_its; 5667cb94ee6SHong Zhang PetscFunctionReturn(0); 5677cb94ee6SHong Zhang } 5687cb94ee6SHong Zhang 5697087cfbeSBarry Smith PetscErrorCode TSSundialsMonitorInternalSteps_Sundials(TS ts,PetscBool s) 5702bfc04deSHong Zhang { 5712bfc04deSHong Zhang TS_Sundials *cvode = (TS_Sundials*)ts->data; 5722bfc04deSHong Zhang 5732bfc04deSHong Zhang PetscFunctionBegin; 5742bfc04deSHong Zhang cvode->monitorstep = s; 5752bfc04deSHong Zhang PetscFunctionReturn(0); 5762bfc04deSHong Zhang } 5777cb94ee6SHong Zhang /* -------------------------------------------------------------------------------------------*/ 5787cb94ee6SHong Zhang 5797cb94ee6SHong Zhang /*@C 5807cb94ee6SHong Zhang TSSundialsGetIterations - Gets the number of nonlinear and linear iterations used so far by Sundials. 5817cb94ee6SHong Zhang 5827cb94ee6SHong Zhang Not Collective 5837cb94ee6SHong Zhang 5847cb94ee6SHong Zhang Input parameters: 5857cb94ee6SHong Zhang . ts - the time-step context 5867cb94ee6SHong Zhang 5877cb94ee6SHong Zhang Output Parameters: 5887cb94ee6SHong Zhang + nonlin - number of nonlinear iterations 5897cb94ee6SHong Zhang - lin - number of linear iterations 5907cb94ee6SHong Zhang 5917cb94ee6SHong Zhang Level: advanced 5927cb94ee6SHong Zhang 5937cb94ee6SHong Zhang Notes: 5947cb94ee6SHong Zhang These return the number since the creation of the TS object 5957cb94ee6SHong Zhang 596f61b2b6aSHong Zhang .seealso: TSSundialsSetType(), TSSundialsSetMaxl(), 5977cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 598f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 599a43b19c4SJed Brown TSSundialsSetLinearTolerance(), TSSundialsGetPC(), TSSetExactFinalTime() 6007cb94ee6SHong Zhang 6017cb94ee6SHong Zhang @*/ 6027087cfbeSBarry Smith PetscErrorCode TSSundialsGetIterations(TS ts,int *nonlin,int *lin) 6037cb94ee6SHong Zhang { 6044ac538c5SBarry Smith PetscErrorCode ierr; 6057cb94ee6SHong Zhang 6067cb94ee6SHong Zhang PetscFunctionBegin; 6074ac538c5SBarry Smith ierr = PetscUseMethod(ts,"TSSundialsGetIterations_C",(TS,int*,int*),(ts,nonlin,lin));CHKERRQ(ierr); 6087cb94ee6SHong Zhang PetscFunctionReturn(0); 6097cb94ee6SHong Zhang } 6107cb94ee6SHong Zhang 6117cb94ee6SHong Zhang /*@ 6127cb94ee6SHong Zhang TSSundialsSetType - Sets the method that Sundials will use for integration. 6137cb94ee6SHong Zhang 6143f9fe445SBarry Smith Logically Collective on TS 6157cb94ee6SHong Zhang 6167cb94ee6SHong Zhang Input parameters: 6177cb94ee6SHong Zhang + ts - the time-step context 6187cb94ee6SHong Zhang - type - one of SUNDIALS_ADAMS or SUNDIALS_BDF 6197cb94ee6SHong Zhang 6207cb94ee6SHong Zhang Level: intermediate 6217cb94ee6SHong Zhang 622f61b2b6aSHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetMaxl(), 6237cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 624f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 6257cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 626a43b19c4SJed Brown TSSetExactFinalTime() 6277cb94ee6SHong Zhang @*/ 6287087cfbeSBarry Smith PetscErrorCode TSSundialsSetType(TS ts,TSSundialsLmmType type) 6297cb94ee6SHong Zhang { 6304ac538c5SBarry Smith PetscErrorCode ierr; 6317cb94ee6SHong Zhang 6327cb94ee6SHong Zhang PetscFunctionBegin; 6334ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetType_C",(TS,TSSundialsLmmType),(ts,type));CHKERRQ(ierr); 6347cb94ee6SHong Zhang PetscFunctionReturn(0); 6357cb94ee6SHong Zhang } 6367cb94ee6SHong Zhang 6377cb94ee6SHong Zhang /*@ 638c4e80e11SFlorian TSSundialsSetMaxord - Sets the maximum order for BDF/Adams method used by SUNDIALS. 639c4e80e11SFlorian 640c4e80e11SFlorian Logically Collective on TS 641c4e80e11SFlorian 642c4e80e11SFlorian Input parameters: 643c4e80e11SFlorian + ts - the time-step context 644c4e80e11SFlorian - maxord - maximum order of BDF / Adams method 645c4e80e11SFlorian 646c4e80e11SFlorian Level: advanced 647c4e80e11SFlorian 648c4e80e11SFlorian .seealso: TSSundialsGetIterations(), TSSundialsSetType(), 649c4e80e11SFlorian TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 650c4e80e11SFlorian TSSundialsGetIterations(), TSSundialsSetType(), 651c4e80e11SFlorian TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 652c4e80e11SFlorian TSSetExactFinalTime() 653c4e80e11SFlorian 654c4e80e11SFlorian @*/ 655c4e80e11SFlorian PetscErrorCode TSSundialsSetMaxord(TS ts,PetscInt maxord) 656c4e80e11SFlorian { 657c4e80e11SFlorian PetscErrorCode ierr; 658c4e80e11SFlorian 659c4e80e11SFlorian PetscFunctionBegin; 660c4e80e11SFlorian PetscValidLogicalCollectiveInt(ts,maxord,2); 661c4e80e11SFlorian ierr = PetscTryMethod(ts,"TSSundialsSetMaxOrd_C",(TS,PetscInt),(ts,maxord));CHKERRQ(ierr); 662c4e80e11SFlorian PetscFunctionReturn(0); 663c4e80e11SFlorian } 664c4e80e11SFlorian 665c4e80e11SFlorian /*@ 666f61b2b6aSHong Zhang TSSundialsSetMaxl - Sets the dimension of the Krylov space used by 6677cb94ee6SHong Zhang GMRES in the linear solver in SUNDIALS. SUNDIALS DOES NOT use restarted GMRES so 668f61b2b6aSHong Zhang this is the maximum number of GMRES steps that will be used. 6697cb94ee6SHong Zhang 6703f9fe445SBarry Smith Logically Collective on TS 6717cb94ee6SHong Zhang 6727cb94ee6SHong Zhang Input parameters: 6737cb94ee6SHong Zhang + ts - the time-step context 674f61b2b6aSHong Zhang - maxl - number of direction vectors (the dimension of Krylov subspace). 6757cb94ee6SHong Zhang 6767cb94ee6SHong Zhang Level: advanced 6777cb94ee6SHong Zhang 6787cb94ee6SHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetType(), 6797cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 680f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 6817cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 682a43b19c4SJed Brown TSSetExactFinalTime() 6837cb94ee6SHong Zhang 6847cb94ee6SHong Zhang @*/ 685f61b2b6aSHong Zhang PetscErrorCode TSSundialsSetMaxl(TS ts,PetscInt maxl) 6867cb94ee6SHong Zhang { 6874ac538c5SBarry Smith PetscErrorCode ierr; 6887cb94ee6SHong Zhang 6897cb94ee6SHong Zhang PetscFunctionBegin; 690f61b2b6aSHong Zhang PetscValidLogicalCollectiveInt(ts,maxl,2); 691f61b2b6aSHong Zhang ierr = PetscTryMethod(ts,"TSSundialsSetMaxl_C",(TS,PetscInt),(ts,maxl));CHKERRQ(ierr); 6927cb94ee6SHong Zhang PetscFunctionReturn(0); 6937cb94ee6SHong Zhang } 6947cb94ee6SHong Zhang 6957cb94ee6SHong Zhang /*@ 6967cb94ee6SHong Zhang TSSundialsSetLinearTolerance - Sets the tolerance used to solve the linear 6977cb94ee6SHong Zhang system by SUNDIALS. 6987cb94ee6SHong Zhang 6993f9fe445SBarry Smith Logically Collective on TS 7007cb94ee6SHong Zhang 7017cb94ee6SHong Zhang Input parameters: 7027cb94ee6SHong Zhang + ts - the time-step context 7037cb94ee6SHong Zhang - tol - the factor by which the tolerance on the nonlinear solver is 7047cb94ee6SHong Zhang multiplied to get the tolerance on the linear solver, .05 by default. 7057cb94ee6SHong Zhang 7067cb94ee6SHong Zhang Level: advanced 7077cb94ee6SHong Zhang 708f61b2b6aSHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetType(), TSSundialsSetMaxl(), 7097cb94ee6SHong Zhang TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 710f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 7117cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 712a43b19c4SJed Brown TSSetExactFinalTime() 7137cb94ee6SHong Zhang 7147cb94ee6SHong Zhang @*/ 7157087cfbeSBarry Smith PetscErrorCode TSSundialsSetLinearTolerance(TS ts,double tol) 7167cb94ee6SHong Zhang { 7174ac538c5SBarry Smith PetscErrorCode ierr; 7187cb94ee6SHong Zhang 7197cb94ee6SHong Zhang PetscFunctionBegin; 720c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,tol,2); 7214ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetLinearTolerance_C",(TS,double),(ts,tol));CHKERRQ(ierr); 7227cb94ee6SHong Zhang PetscFunctionReturn(0); 7237cb94ee6SHong Zhang } 7247cb94ee6SHong Zhang 7257cb94ee6SHong Zhang /*@ 7267cb94ee6SHong Zhang TSSundialsSetGramSchmidtType - Sets type of orthogonalization used 7277cb94ee6SHong Zhang in GMRES method by SUNDIALS linear solver. 7287cb94ee6SHong Zhang 7293f9fe445SBarry Smith Logically Collective on TS 7307cb94ee6SHong Zhang 7317cb94ee6SHong Zhang Input parameters: 7327cb94ee6SHong Zhang + ts - the time-step context 7337cb94ee6SHong Zhang - type - either SUNDIALS_MODIFIED_GS or SUNDIALS_CLASSICAL_GS 7347cb94ee6SHong Zhang 7357cb94ee6SHong Zhang Level: advanced 7367cb94ee6SHong Zhang 737f61b2b6aSHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetType(), TSSundialsSetMaxl(), 7387cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), 739f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 7407cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 741a43b19c4SJed Brown TSSetExactFinalTime() 7427cb94ee6SHong Zhang 7437cb94ee6SHong Zhang @*/ 7447087cfbeSBarry Smith PetscErrorCode TSSundialsSetGramSchmidtType(TS ts,TSSundialsGramSchmidtType type) 7457cb94ee6SHong Zhang { 7464ac538c5SBarry Smith PetscErrorCode ierr; 7477cb94ee6SHong Zhang 7487cb94ee6SHong Zhang PetscFunctionBegin; 7494ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetGramSchmidtType_C",(TS,TSSundialsGramSchmidtType),(ts,type));CHKERRQ(ierr); 7507cb94ee6SHong Zhang PetscFunctionReturn(0); 7517cb94ee6SHong Zhang } 7527cb94ee6SHong Zhang 7537cb94ee6SHong Zhang /*@ 7547cb94ee6SHong Zhang TSSundialsSetTolerance - Sets the absolute and relative tolerance used by 7557cb94ee6SHong Zhang Sundials for error control. 7567cb94ee6SHong Zhang 7573f9fe445SBarry Smith Logically Collective on TS 7587cb94ee6SHong Zhang 7597cb94ee6SHong Zhang Input parameters: 7607cb94ee6SHong Zhang + ts - the time-step context 7617cb94ee6SHong Zhang . aabs - the absolute tolerance 7627cb94ee6SHong Zhang - rel - the relative tolerance 7637cb94ee6SHong Zhang 7647cb94ee6SHong Zhang See the Cvode/Sundials users manual for exact details on these parameters. Essentially 7657cb94ee6SHong Zhang these regulate the size of the error for a SINGLE timestep. 7667cb94ee6SHong Zhang 7677cb94ee6SHong Zhang Level: intermediate 7687cb94ee6SHong Zhang 769f61b2b6aSHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetType(), TSSundialsSetGMRESMaxl(), 7707cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), 771f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 7727cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC(), 773a43b19c4SJed Brown TSSetExactFinalTime() 7747cb94ee6SHong Zhang 7757cb94ee6SHong Zhang @*/ 7767087cfbeSBarry Smith PetscErrorCode TSSundialsSetTolerance(TS ts,double aabs,double rel) 7777cb94ee6SHong Zhang { 7784ac538c5SBarry Smith PetscErrorCode ierr; 7797cb94ee6SHong Zhang 7807cb94ee6SHong Zhang PetscFunctionBegin; 7814ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetTolerance_C",(TS,double,double),(ts,aabs,rel));CHKERRQ(ierr); 7827cb94ee6SHong Zhang PetscFunctionReturn(0); 7837cb94ee6SHong Zhang } 7847cb94ee6SHong Zhang 7857cb94ee6SHong Zhang /*@ 7867cb94ee6SHong Zhang TSSundialsGetPC - Extract the PC context from a time-step context for Sundials. 7877cb94ee6SHong Zhang 7887cb94ee6SHong Zhang Input Parameter: 7897cb94ee6SHong Zhang . ts - the time-step context 7907cb94ee6SHong Zhang 7917cb94ee6SHong Zhang Output Parameter: 7927cb94ee6SHong Zhang . pc - the preconditioner context 7937cb94ee6SHong Zhang 7947cb94ee6SHong Zhang Level: advanced 7957cb94ee6SHong Zhang 796f61b2b6aSHong Zhang .seealso: TSSundialsGetIterations(), TSSundialsSetType(), TSSundialsSetMaxl(), 7977cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 798f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 7997cb94ee6SHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance() 8007cb94ee6SHong Zhang @*/ 8017087cfbeSBarry Smith PetscErrorCode TSSundialsGetPC(TS ts,PC *pc) 8027cb94ee6SHong Zhang { 8034ac538c5SBarry Smith PetscErrorCode ierr; 8047cb94ee6SHong Zhang 8057cb94ee6SHong Zhang PetscFunctionBegin; 8064ac538c5SBarry Smith ierr = PetscUseMethod(ts,"TSSundialsGetPC_C",(TS,PC*),(ts,pc));CHKERRQ(ierr); 8077cb94ee6SHong Zhang PetscFunctionReturn(0); 8087cb94ee6SHong Zhang } 8097cb94ee6SHong Zhang 810f1cd61daSJed Brown /*@ 811f1cd61daSJed Brown TSSundialsSetMinTimeStep - Smallest time step to be chosen by the adaptive controller. 812f1cd61daSJed Brown 813*d8d19677SJose E. Roman Input Parameters: 814f1cd61daSJed Brown + ts - the time-step context 815f1cd61daSJed Brown - mindt - lowest time step if positive, negative to deactivate 816f1cd61daSJed Brown 817fc6b9e64SJed Brown Note: 818fc6b9e64SJed Brown Sundials will error if it is not possible to keep the estimated truncation error below 819fc6b9e64SJed Brown the tolerance set with TSSundialsSetTolerance() without going below this step size. 820fc6b9e64SJed Brown 821f1cd61daSJed Brown Level: beginner 822f1cd61daSJed Brown 823f1cd61daSJed Brown .seealso: TSSundialsSetType(), TSSundialsSetTolerance(), 824f1cd61daSJed Brown @*/ 8257087cfbeSBarry Smith PetscErrorCode TSSundialsSetMinTimeStep(TS ts,PetscReal mindt) 826f1cd61daSJed Brown { 8274ac538c5SBarry Smith PetscErrorCode ierr; 828f1cd61daSJed Brown 829f1cd61daSJed Brown PetscFunctionBegin; 8304ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetMinTimeStep_C",(TS,PetscReal),(ts,mindt));CHKERRQ(ierr); 831f1cd61daSJed Brown PetscFunctionReturn(0); 832f1cd61daSJed Brown } 833f1cd61daSJed Brown 834f1cd61daSJed Brown /*@ 835f1cd61daSJed Brown TSSundialsSetMaxTimeStep - Largest time step to be chosen by the adaptive controller. 836f1cd61daSJed Brown 837*d8d19677SJose E. Roman Input Parameters: 838f1cd61daSJed Brown + ts - the time-step context 839f1cd61daSJed Brown - maxdt - lowest time step if positive, negative to deactivate 840f1cd61daSJed Brown 841f1cd61daSJed Brown Level: beginner 842f1cd61daSJed Brown 843f1cd61daSJed Brown .seealso: TSSundialsSetType(), TSSundialsSetTolerance(), 844f1cd61daSJed Brown @*/ 8457087cfbeSBarry Smith PetscErrorCode TSSundialsSetMaxTimeStep(TS ts,PetscReal maxdt) 846f1cd61daSJed Brown { 8474ac538c5SBarry Smith PetscErrorCode ierr; 848f1cd61daSJed Brown 849f1cd61daSJed Brown PetscFunctionBegin; 8504ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsSetMaxTimeStep_C",(TS,PetscReal),(ts,maxdt));CHKERRQ(ierr); 851f1cd61daSJed Brown PetscFunctionReturn(0); 852f1cd61daSJed Brown } 853f1cd61daSJed Brown 8542bfc04deSHong Zhang /*@ 8552bfc04deSHong Zhang TSSundialsMonitorInternalSteps - Monitor Sundials internal steps (Defaults to false). 8562bfc04deSHong Zhang 857*d8d19677SJose E. Roman Input Parameters: 8582bfc04deSHong Zhang + ts - the time-step context 8592bfc04deSHong Zhang - ft - PETSC_TRUE if monitor, else PETSC_FALSE 8602bfc04deSHong Zhang 8612bfc04deSHong Zhang Level: beginner 8622bfc04deSHong Zhang 863f61b2b6aSHong Zhang .seealso:TSSundialsGetIterations(), TSSundialsSetType(), TSSundialsSetMaxl(), 8642bfc04deSHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), 865f61b2b6aSHong Zhang TSSundialsGetIterations(), TSSundialsSetType(), 8662bfc04deSHong Zhang TSSundialsSetLinearTolerance(), TSSundialsSetTolerance(), TSSundialsGetPC() 8672bfc04deSHong Zhang @*/ 8687087cfbeSBarry Smith PetscErrorCode TSSundialsMonitorInternalSteps(TS ts,PetscBool ft) 8692bfc04deSHong Zhang { 8704ac538c5SBarry Smith PetscErrorCode ierr; 8712bfc04deSHong Zhang 8722bfc04deSHong Zhang PetscFunctionBegin; 8734ac538c5SBarry Smith ierr = PetscTryMethod(ts,"TSSundialsMonitorInternalSteps_C",(TS,PetscBool),(ts,ft));CHKERRQ(ierr); 8742bfc04deSHong Zhang PetscFunctionReturn(0); 8752bfc04deSHong Zhang } 8767cb94ee6SHong Zhang /* -------------------------------------------------------------------------------------------*/ 8777cb94ee6SHong Zhang /*MC 87896f5712cSJed Brown TSSUNDIALS - ODE solver using the LLNL CVODE/SUNDIALS package (now called SUNDIALS) 8797cb94ee6SHong Zhang 8807cb94ee6SHong Zhang Options Database: 881897c9f78SHong Zhang + -ts_sundials_type <bdf,adams> - 8827cb94ee6SHong Zhang . -ts_sundials_gramschmidt_type <modified, classical> - type of orthogonalization inside GMRES 8837cb94ee6SHong Zhang . -ts_sundials_atol <tol> - Absolute tolerance for convergence 8847cb94ee6SHong Zhang . -ts_sundials_rtol <tol> - Relative tolerance for convergence 885897c9f78SHong Zhang . -ts_sundials_linear_tolerance <tol> - 886f61b2b6aSHong Zhang . -ts_sundials_maxl <maxl> - Max dimension of the Krylov subspace 887897c9f78SHong Zhang - -ts_sundials_monitor_steps - Monitor SUNDIALS internal steps 88816016685SHong Zhang 88995452b02SPatrick Sanan Notes: 89095452b02SPatrick Sanan This uses its own nonlinear solver and Krylov method so PETSc SNES and KSP options do not apply, 891897c9f78SHong Zhang only PETSc PC options. 8927cb94ee6SHong Zhang 8937cb94ee6SHong Zhang Level: beginner 8947cb94ee6SHong Zhang 895f61b2b6aSHong Zhang .seealso: TSCreate(), TS, TSSetType(), TSSundialsSetType(), TSSundialsSetMaxl(), TSSundialsSetLinearTolerance(), 896a43b19c4SJed Brown TSSundialsSetGramSchmidtType(), TSSundialsSetTolerance(), TSSundialsGetPC(), TSSundialsGetIterations(), TSSetExactFinalTime() 8977cb94ee6SHong Zhang 8987cb94ee6SHong Zhang M*/ 8998cc058d9SJed Brown PETSC_EXTERN PetscErrorCode TSCreate_Sundials(TS ts) 9007cb94ee6SHong Zhang { 9017cb94ee6SHong Zhang TS_Sundials *cvode; 9027cb94ee6SHong Zhang PetscErrorCode ierr; 90342528757SHong Zhang PC pc; 9047cb94ee6SHong Zhang 9057cb94ee6SHong Zhang PetscFunctionBegin; 906277b19d0SLisandro Dalcin ts->ops->reset = TSReset_Sundials; 90728adb3f7SHong Zhang ts->ops->destroy = TSDestroy_Sundials; 90828adb3f7SHong Zhang ts->ops->view = TSView_Sundials; 909214bc6a2SJed Brown ts->ops->setup = TSSetUp_Sundials; 910b4eba00bSSean Farley ts->ops->step = TSStep_Sundials; 911b4eba00bSSean Farley ts->ops->interpolate = TSInterpolate_Sundials; 912214bc6a2SJed Brown ts->ops->setfromoptions = TSSetFromOptions_Sundials; 9132ffb9264SLisandro Dalcin ts->default_adapt_type = TSADAPTNONE; 9147cb94ee6SHong Zhang 915b00a9115SJed Brown ierr = PetscNewLog(ts,&cvode);CHKERRQ(ierr); 916bbd56ea5SKarl Rupp 917efd4aadfSBarry Smith ts->usessnes = PETSC_TRUE; 918efd4aadfSBarry Smith 9197cb94ee6SHong Zhang ts->data = (void*)cvode; 9206fadb2cdSHong Zhang cvode->cvode_type = SUNDIALS_BDF; 9216fadb2cdSHong Zhang cvode->gtype = SUNDIALS_CLASSICAL_GS; 922f61b2b6aSHong Zhang cvode->maxl = 5; 923c4e80e11SFlorian cvode->maxord = PETSC_DEFAULT; 9247cb94ee6SHong Zhang cvode->linear_tol = .05; 925b4eba00bSSean Farley cvode->monitorstep = PETSC_TRUE; 9267cb94ee6SHong Zhang 927ffc4695bSBarry Smith ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)ts),&(cvode->comm_sundials));CHKERRMPI(ierr); 928f1cd61daSJed Brown 929f1cd61daSJed Brown cvode->mindt = -1.; 930f1cd61daSJed Brown cvode->maxdt = -1.; 931f1cd61daSJed Brown 9327cb94ee6SHong Zhang /* set tolerance for Sundials */ 9337cb94ee6SHong Zhang cvode->reltol = 1e-6; 9342c823083SHong Zhang cvode->abstol = 1e-6; 9357cb94ee6SHong Zhang 93642528757SHong Zhang /* set PCNONE as default pctype */ 93742528757SHong Zhang ierr = TSSundialsGetPC_Sundials(ts,&pc);CHKERRQ(ierr); 93842528757SHong Zhang ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 93942528757SHong Zhang 940bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetType_C",TSSundialsSetType_Sundials);CHKERRQ(ierr); 941bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxl_C",TSSundialsSetMaxl_Sundials);CHKERRQ(ierr); 942bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetLinearTolerance_C",TSSundialsSetLinearTolerance_Sundials);CHKERRQ(ierr); 943bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetGramSchmidtType_C",TSSundialsSetGramSchmidtType_Sundials);CHKERRQ(ierr); 944bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetTolerance_C",TSSundialsSetTolerance_Sundials);CHKERRQ(ierr); 945bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMinTimeStep_C",TSSundialsSetMinTimeStep_Sundials);CHKERRQ(ierr); 946bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxTimeStep_C",TSSundialsSetMaxTimeStep_Sundials);CHKERRQ(ierr); 947bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetPC_C",TSSundialsGetPC_Sundials);CHKERRQ(ierr); 948bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetIterations_C",TSSundialsGetIterations_Sundials);CHKERRQ(ierr); 949bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsMonitorInternalSteps_C",TSSundialsMonitorInternalSteps_Sundials);CHKERRQ(ierr); 9507cb94ee6SHong Zhang PetscFunctionReturn(0); 9517cb94ee6SHong Zhang } 952