static int SolveIt(void *kmem, N_Vector u, N_Vector s, int glstr, int mset) { int flag; printf("\n"); if (mset==1) printf("Exact Newton"); else printf("Modified Newton"); if (glstr == KIN_NONE) printf("\n"); else printf(" with line search\n"); flag = KINSetMaxSetupCalls(kmem, mset); if (check_flag(&flag, "KINSetMaxSetupCalls", 1)) return(1); flag = KINSol(kmem, u, glstr, s, s); if (check_flag(&flag, "KINSol", 1)) return(1); printf("Solution:\n [x1,x2] = "); PrintOutput(u); PrintFinalStats(kmem); return(0); }
void AsyncExeCmd::OnTimer(wxTimerEvent &event) { wxUnusedVar(event); if( m_stop ){ m_proc->Terminate(); return; } PrintOutput(); }
void Display(void) { glClear(GL_COLOR_BUFFER_BIT); DrawAllButtons(); if (stat.turnedON) { PrintOutput(); } glFlush(); glutSwapBuffers(); }
int main(void) { realtype dx, dy, reltol, abstol, t, tout, umax; N_Vector u, up; UserData data; void *cvode_mem; int iout, flag; long int nst; u = NULL; data = NULL; cvode_mem = NULL; u = N_VNew_Serial(NEQ); up = N_VNew_Serial(NEQ); reltol = ZERO; abstol = ATOL; data = (UserData) malloc(sizeof *data); dx = data->dx = XMAX/(MX+1); dy = data->dy = YMAX/(MY+1); data->hdcoef = ONE/(dx*dx); data->hacoef = HALF/(TWO*dx); data->vdcoef = ONE/(dy*dy); SetIC(u, data); cvode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cvode_mem, (void *)f, data, T0, u, NULL, CP_SS, reltol, &abstol); flag = CPLapackBand(cvode_mem, NEQ, MY, MY); flag = CPDlsSetJacFn(cvode_mem, (void *)Jac, data); /* In loop over output points: call CPode, print results, test for errors */ umax = N_VMaxNorm(u); PrintHeader(reltol, abstol, umax); for(iout=1, tout=T1; iout <= NOUT; iout++, tout += DTOUT) { flag = CPode(cvode_mem, tout, &t, u, up, CP_NORMAL); umax = N_VMaxNorm(u); flag = CPodeGetNumSteps(cvode_mem, &nst); PrintOutput(t, umax, nst); } PrintFinalStats(cvode_mem); N_VDestroy_Serial(u); CPodeFree(&cvode_mem); free(data); return(0); }
void AsyncExeCmd::ProcessEnd(wxProcessEvent& event) { //read all input before stopping the timer if( !m_stop ){ PrintOutput(); } m_timer->Stop(); m_busy = false; m_stop = false; SendEndMsg(event.GetExitCode()); }
void rbBytecode::Parse() { wxLogMessage(wxT("")); // Initialize stack // parse until end-script marker Root = new rbToken(NULL,-1,wxT("ROOT"),wxT("ROOT")); PushIndent(); PrintOutput(wxT("{\n")); PushStack(Root); while( true ) { dword token = LoadToken(); if( token == 0x53 ) break; } PopStack(); PopIndent(); //ScriptText.RemoveLast(); // remove tab PrintOutput( wxT("}\n") ); }
INT wmain(INT argc, WCHAR **argv) { po::variables_map variables_map; printInfoStruct stPrintInfo; if (!ParseArguments(argc, argv, variables_map, stPrintInfo)) return 3; if (variables_map.count("print-objects")) { PrintObjects(); return 0; } if (variables_map.count("print-object-info")) { PrintObjectInfo(stPrintInfo); return 0; } if (QueryPerfData(stPrintInfo)) return PrintOutput(variables_map, stPrintInfo); else return 3; }
int main(void) { realtype dx, dy, reltol, abstol, t, tout, umax; N_Vector u; UserData data; void *cvode_mem; int iout, flag; long int nst; u = NULL; data = NULL; cvode_mem = NULL; /* Create a serial vector */ u = N_VNew_Serial(NEQ); /* Allocate u vector */ if(check_flag((void*)u, "N_VNew_Serial", 0)) return(1); reltol = ZERO; /* Set the tolerances */ abstol = ATOL; data = (UserData) malloc(sizeof *data); /* Allocate data memory */ if(check_flag((void *)data, "malloc", 2)) return(1); dx = data->dx = XMAX/(MX+1); /* Set grid coefficients in data */ dy = data->dy = YMAX/(MY+1); data->hdcoef = ONE/(dx*dx); data->hacoef = HALF/(TWO*dx); data->vdcoef = ONE/(dy*dy); SetIC(u, data); /* Initialize u vector */ /* Call CVodeCreate to create the solver memory and specify the * Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in u'=f(t,u), the inital time T0, and * the initial dependent variable vector u. */ flag = CVodeInit(cvode_mem, f, T0, u); if(check_flag(&flag, "CVodeInit", 1)) return(1); /* Call CVodeSStolerances to specify the scalar relative tolerance * and scalar absolute tolerance */ flag = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSStolerances", 1)) return(1); /* Set the pointer to user-defined data */ flag = CVodeSetUserData(cvode_mem, data); if(check_flag(&flag, "CVodeSetUserData", 1)) return(1); /* Call CVLapackBand to specify the CVBAND band linear solver */ flag = CVLapackBand(cvode_mem, NEQ, MY, MY); if(check_flag(&flag, "CVLapackBand", 1)) return(1); /* Set the user-supplied Jacobian routine Jac */ flag = CVDlsSetBandJacFn(cvode_mem, Jac); if(check_flag(&flag, "CVDlsSetBandJacFn", 1)) return(1); /* In loop over output points: call CVode, print results, test for errors */ umax = N_VMaxNorm(u); PrintHeader(reltol, abstol, umax); for(iout=1, tout=T1; iout <= NOUT; iout++, tout += DTOUT) { flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL); if(check_flag(&flag, "CVode", 1)) break; umax = N_VMaxNorm(u); flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); PrintOutput(t, umax, nst); } PrintFinalStats(cvode_mem); /* Print some final statistics */ N_VDestroy_Serial(u); /* Free the u vector */ CVodeFree(&cvode_mem); /* Free the integrator memory */ free(data); /* Free the user data */ return(0); }
int main(int argc, char *argv[]) { MPI_Comm comm; void *mem; UserData webdata; long int SystemSize, local_N, mudq, mldq, mukeep, mlkeep; realtype rtol, atol, t0, tout, tret; N_Vector cc, cp, res, id; int thispe, npes, maxl, iout, retval; cc = cp = res = id = NULL; webdata = NULL; mem = NULL; /* Set communicator, and get processor number and total number of PE's. */ MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_rank(comm, &thispe); MPI_Comm_size(comm, &npes); if (npes != NPEX*NPEY) { if (thispe == 0) fprintf(stderr, "\nMPI_ERROR(0): npes = %d not equal to NPEX*NPEY = %d\n", npes, NPEX*NPEY); MPI_Finalize(); return(1); } /* Set local length (local_N) and global length (SystemSize). */ local_N = MXSUB*MYSUB*NUM_SPECIES; SystemSize = NEQ; /* Set up user data block webdata. */ webdata = (UserData) malloc(sizeof *webdata); webdata->rates = N_VNew_Parallel(comm, local_N, SystemSize); webdata->acoef = newDenseMat(NUM_SPECIES, NUM_SPECIES); InitUserData(webdata, thispe, npes, comm); /* Create needed vectors, and load initial values. The vector res is used temporarily only. */ cc = N_VNew_Parallel(comm, local_N, SystemSize); if(check_flag((void *)cc, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); cp = N_VNew_Parallel(comm, local_N, SystemSize); if(check_flag((void *)cp, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); res = N_VNew_Parallel(comm, local_N, SystemSize); if(check_flag((void *)res, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); id = N_VNew_Parallel(comm, local_N, SystemSize); if(check_flag((void *)id, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); SetInitialProfiles(cc, cp, id, res, webdata); N_VDestroy_Parallel(res); /* Set remaining inputs to IDAMalloc. */ t0 = ZERO; rtol = RTOL; atol = ATOL; /* Call IDACreate and IDAMalloc to initialize solution */ mem = IDACreate(); if(check_flag((void *)mem, "IDACreate", 0, thispe)) MPI_Abort(comm, 1); retval = IDASetUserData(mem, webdata); if(check_flag(&retval, "IDASetUserData", 1, thispe)) MPI_Abort(comm, 1); retval = IDASetId(mem, id); if(check_flag(&retval, "IDASetId", 1, thispe)) MPI_Abort(comm, 1); retval = IDAInit(mem, resweb, t0, cc, cp); if(check_flag(&retval, "IDAInit", 1, thispe)) MPI_Abort(comm, 1); retval = IDASStolerances(mem, rtol, atol); if(check_flag(&retval, "IDASStolerances", 1, thispe)) MPI_Abort(comm, 1); /* Call IDASpgmr to specify the IDA linear solver IDASPGMR */ maxl = 16; retval = IDASpgmr(mem, maxl); if(check_flag(&retval, "IDASpgmr", 1, thispe)) MPI_Abort(comm, 1); /* Call IDABBDPrecInit to initialize the band-block-diagonal preconditioner. The half-bandwidths for the difference quotient evaluation are exact for the system Jacobian, but only a 5-diagonal band matrix is retained. */ mudq = mldq = NSMXSUB; mukeep = mlkeep = 2; retval = IDABBDPrecInit(mem, local_N, mudq, mldq, mukeep, mlkeep, ZERO, reslocal, NULL); if(check_flag(&retval, "IDABBDPrecInit", 1, thispe)) MPI_Abort(comm, 1); /* Call IDACalcIC (with default options) to correct the initial values. */ tout = RCONST(0.001); retval = IDACalcIC(mem, IDA_YA_YDP_INIT, tout); if(check_flag(&retval, "IDACalcIC", 1, thispe)) MPI_Abort(comm, 1); /* On PE 0, print heading, basic parameters, initial values. */ if (thispe == 0) PrintHeader(SystemSize, maxl, mudq, mldq, mukeep, mlkeep, rtol, atol); PrintOutput(mem, cc, t0, webdata, comm); /* Call IDA in tout loop, normal mode, and print selected output. */ for (iout = 1; iout <= NOUT; iout++) { retval = IDASolve(mem, tout, &tret, cc, cp, IDA_NORMAL); if(check_flag(&retval, "IDASolve", 1, thispe)) MPI_Abort(comm, 1); PrintOutput(mem, cc, tret, webdata, comm); if (iout < 3) tout *= TMULT; else tout += TADD; } /* On PE 0, print final set of statistics. */ if (thispe == 0) PrintFinalStats(mem); /* Free memory. */ N_VDestroy_Parallel(cc); N_VDestroy_Parallel(cp); N_VDestroy_Parallel(id); IDAFree(&mem); destroyMat(webdata->acoef); N_VDestroy_Parallel(webdata->rates); free(webdata); MPI_Finalize(); return(0); }
int main(int argc, char *argv[]) { MPI_Comm comm; void *kmem; UserData data; N_Vector cc, sc, constraints; int globalstrategy; long int Nlocal; realtype fnormtol, scsteptol, dq_rel_uu; int flag, maxl, maxlrst; long int mudq, mldq, mukeep, mlkeep; int my_pe, npes, npelast = NPEX*NPEY-1; data = NULL; kmem = NULL; cc = sc = constraints = NULL; /* Get processor number and total number of pe's */ MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &npes); MPI_Comm_rank(comm, &my_pe); if (npes != NPEX*NPEY) { if (my_pe == 0) printf("\nMPI_ERROR(0): npes=%d is not equal to NPEX*NPEY=%d\n", npes, NPEX*NPEY); return(1); } /* Allocate memory, and set problem data, initial values, tolerances */ /* Set local length */ Nlocal = NUM_SPECIES*MXSUB*MYSUB; /* Allocate and initialize user data block */ data = AllocUserData(); if (check_flag((void *)data, "AllocUserData", 2, my_pe)) MPI_Abort(comm, 1); InitUserData(my_pe, Nlocal, comm, data); /* Choose global strategy */ globalstrategy = KIN_NONE; /* Allocate and initialize vectors */ cc = N_VNew_Parallel(comm, Nlocal, NEQ); if (check_flag((void *)cc, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); sc = N_VNew_Parallel(comm, Nlocal, NEQ); if (check_flag((void *)sc, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); data->rates = N_VNew_Parallel(comm, Nlocal, NEQ); if (check_flag((void *)data->rates, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); constraints = N_VNew_Parallel(comm, Nlocal, NEQ); if (check_flag((void *)constraints, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); N_VConst(ZERO, constraints); SetInitialProfiles(cc, sc); fnormtol = FTOL; scsteptol = STOL; /* Call KINCreate/KINInit to initialize KINSOL: nvSpec points to machine environment data A pointer to KINSOL problem memory is returned and stored in kmem. */ kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0, my_pe)) MPI_Abort(comm, 1); /* Vector cc passed as template vector. */ flag = KINInit(kmem, func, cc); if (check_flag(&flag, "KINInit", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetUserData(kmem, data); if (check_flag(&flag, "KINSetUserData", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetConstraints(kmem, constraints); if (check_flag(&flag, "KINSetConstraints", 1, my_pe)) MPI_Abort(comm, 1); /* We no longer need the constraints vector since KINSetConstraints creates a private copy for KINSOL to use. */ N_VDestroy_Parallel(constraints); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1, my_pe)) MPI_Abort(comm, 1); /* Call KINBBDPrecInit to initialize and allocate memory for the band-block-diagonal preconditioner, and specify the local and communication functions func_local and gcomm=NULL (all communication needed for the func_local is already done in func). */ dq_rel_uu = ZERO; mudq = mldq = 2*NUM_SPECIES - 1; mukeep = mlkeep = NUM_SPECIES; /* Call KINBBDSpgmr to specify the linear solver KINSPGMR */ maxl = 20; maxlrst = 2; flag = KINSpgmr(kmem, maxl); if (check_flag(&flag, "KINSpgmr", 1, my_pe)) MPI_Abort(comm, 1); /* Initialize BBD preconditioner */ flag = KINBBDPrecInit(kmem, Nlocal, mudq, mldq, mukeep, mlkeep, dq_rel_uu, func_local, NULL); if (check_flag(&flag, "KINBBDPrecInit", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSpilsSetMaxRestarts(kmem, maxlrst); if (check_flag(&flag, "KINSpilsSetMaxRestarts", 1, my_pe)) MPI_Abort(comm, 1); /* Print out the problem size, solution parameters, initial guess. */ if (my_pe == 0) PrintHeader(globalstrategy, maxl, maxlrst, mudq, mldq, mukeep, mlkeep, fnormtol, scsteptol); /* call KINSol and print output concentration profile */ flag = KINSol(kmem, /* KINSol memory block */ cc, /* initial guesss on input; solution vector */ globalstrategy, /* global stragegy choice */ sc, /* scaling vector, for the variable cc */ sc); /* scaling vector for function values fval */ if (check_flag(&flag, "KINSol", 1, my_pe)) MPI_Abort(comm, 1); if (my_pe == 0) printf("\n\nComputed equilibrium species concentrations:\n"); if (my_pe == 0 || my_pe==npelast) PrintOutput(my_pe, comm, cc); /* Print final statistics and free memory */ if (my_pe == 0) PrintFinalStats(kmem); N_VDestroy_Parallel(cc); N_VDestroy_Parallel(sc); KINFree(&kmem); FreeUserData(data); MPI_Finalize(); return(0); }
int main(int argc, char *argv[]) { UserData data; void *cvode_mem; SUNMatrix A, AB; SUNLinearSolver LS, LSB; realtype dx, dy, reltol, abstol, t; N_Vector u; int indexB; realtype reltolB, abstolB; N_Vector uB; int retval, ncheck; data = NULL; cvode_mem = NULL; u = uB = NULL; LS = LSB = NULL; A = AB = NULL; /* Allocate and initialize user data memory */ data = (UserData) malloc(sizeof *data); if(check_retval((void *)data, "malloc", 2)) return(1); dx = data->dx = XMAX/(MX+1); dy = data->dy = YMAX/(MY+1); data->hdcoef = ONE/(dx*dx); data->hacoef = RCONST(1.5)/(TWO*dx); data->vdcoef = ONE/(dy*dy); /* Set the tolerances for the forward integration */ reltol = ZERO; abstol = ATOL; /* Allocate u vector */ u = N_VNew_Serial(NEQ); if(check_retval((void *)u, "N_VNew", 0)) return(1); /* Initialize u vector */ SetIC(u, data); /* Create and allocate CVODES memory for forward run */ printf("\nCreate and allocate CVODES memory for forward runs\n"); cvode_mem = CVodeCreate(CV_BDF); if(check_retval((void *)cvode_mem, "CVodeCreate", 0)) return(1); retval = CVodeSetUserData(cvode_mem, data); if(check_retval(&retval, "CVodeSetUserData", 1)) return(1); retval = CVodeInit(cvode_mem, f, T0, u); if(check_retval(&retval, "CVodeInit", 1)) return(1); retval = CVodeSStolerances(cvode_mem, reltol, abstol); if(check_retval(&retval, "CVodeSStolerances", 1)) return(1); /* Create banded SUNMatrix for the forward problem */ A = SUNBandMatrix(NEQ, MY, MY); if(check_retval((void *)A, "SUNBandMatrix", 0)) return(1); /* Create banded SUNLinearSolver for the forward problem */ LS = SUNLinSol_Band(u, A); if(check_retval((void *)LS, "SUNLinSol_Band", 0)) return(1); /* Attach the matrix and linear solver */ retval = CVodeSetLinearSolver(cvode_mem, LS, A); if(check_retval(&retval, "CVodeSetLinearSolver", 1)) return(1); /* Set the user-supplied Jacobian routine for the forward problem */ retval = CVodeSetJacFn(cvode_mem, Jac); if(check_retval(&retval, "CVodeSetJacFn", 1)) return(1); /* Allocate global memory */ printf("\nAllocate global memory\n"); retval = CVodeAdjInit(cvode_mem, NSTEP, CV_HERMITE); if(check_retval(&retval, "CVodeAdjInit", 1)) return(1); /* Perform forward run */ printf("\nForward integration\n"); retval = CVodeF(cvode_mem, TOUT, u, &t, CV_NORMAL, &ncheck); if(check_retval(&retval, "CVodeF", 1)) return(1); printf("\nncheck = %d\n", ncheck); /* Set the tolerances for the backward integration */ reltolB = RTOLB; abstolB = ATOL; /* Allocate uB */ uB = N_VNew_Serial(NEQ); if(check_retval((void *)uB, "N_VNew", 0)) return(1); /* Initialize uB = 0 */ N_VConst(ZERO, uB); /* Create and allocate CVODES memory for backward run */ printf("\nCreate and allocate CVODES memory for backward run\n"); retval = CVodeCreateB(cvode_mem, CV_BDF, &indexB); if(check_retval(&retval, "CVodeCreateB", 1)) return(1); retval = CVodeSetUserDataB(cvode_mem, indexB, data); if(check_retval(&retval, "CVodeSetUserDataB", 1)) return(1); retval = CVodeInitB(cvode_mem, indexB, fB, TOUT, uB); if(check_retval(&retval, "CVodeInitB", 1)) return(1); retval = CVodeSStolerancesB(cvode_mem, indexB, reltolB, abstolB); if(check_retval(&retval, "CVodeSStolerancesB", 1)) return(1); /* Create banded SUNMatrix for the backward problem */ AB = SUNBandMatrix(NEQ, MY, MY); if(check_retval((void *)AB, "SUNBandMatrix", 0)) return(1); /* Create banded SUNLinearSolver for the backward problem */ LSB = SUNLinSol_Band(uB, AB); if(check_retval((void *)LSB, "SUNLinSol_Band", 0)) return(1); /* Attach the matrix and linear solver */ retval = CVodeSetLinearSolverB(cvode_mem, indexB, LSB, AB); if(check_retval(&retval, "CVodeSetLinearSolverB", 1)) return(1); /* Set the user-supplied Jacobian routine for the backward problem */ retval = CVodeSetJacFnB(cvode_mem, indexB, JacB); if(check_retval(&retval, "CVodeSetJacFnB", 1)) return(1); /* Perform backward integration */ printf("\nBackward integration\n"); retval = CVodeB(cvode_mem, T0, CV_NORMAL); if(check_retval(&retval, "CVodeB", 1)) return(1); retval = CVodeGetB(cvode_mem, indexB, &t, uB); if(check_retval(&retval, "CVodeGetB", 1)) return(1); PrintOutput(uB, data); N_VDestroy(u); /* Free the u vector */ N_VDestroy(uB); /* Free the uB vector */ CVodeFree(&cvode_mem); /* Free the CVODE problem memory */ SUNLinSolFree(LS); /* Free the forward linear solver memory */ SUNMatDestroy(A); /* Free the forward matrix memory */ SUNLinSolFree(LSB); /* Free the backward linear solver memory */ SUNMatDestroy(AB); /* Free the backward matrix memory */ free(data); /* Free the user data */ return(0); }
int main(void) { void *mem; N_Vector yy, yp, avtol; realtype rtol, *yval, *ypval, *atval; realtype t0, tout1, tout, tret; int iout, retval, retvalr; int rootsfound[2]; SUNMatrix A; SUNLinearSolver LS; sunindextype nnz; mem = NULL; yy = yp = avtol = NULL; yval = ypval = atval = NULL; A = NULL; LS = NULL; /* Allocate N-vectors. */ yy = N_VNew_Serial(NEQ); if(check_retval((void *)yy, "N_VNew_Serial", 0)) return(1); yp = N_VNew_Serial(NEQ); if(check_retval((void *)yp, "N_VNew_Serial", 0)) return(1); avtol = N_VNew_Serial(NEQ); if(check_retval((void *)avtol, "N_VNew_Serial", 0)) return(1); /* Create and initialize y, y', and absolute tolerance vectors. */ yval = N_VGetArrayPointer(yy); yval[0] = ONE; yval[1] = ZERO; yval[2] = ZERO; ypval = N_VGetArrayPointer(yp); ypval[0] = RCONST(-0.04); ypval[1] = RCONST(0.04); ypval[2] = ZERO; rtol = RCONST(1.0e-4); atval = N_VGetArrayPointer(avtol); atval[0] = RCONST(1.0e-8); atval[1] = RCONST(1.0e-6); atval[2] = RCONST(1.0e-6); /* Integration limits */ t0 = ZERO; tout1 = RCONST(0.4); PrintHeader(rtol, avtol, yy); /* Call IDACreate and IDAInit to initialize IDA memory */ mem = IDACreate(); if(check_retval((void *)mem, "IDACreate", 0)) return(1); retval = IDAInit(mem, resrob, t0, yy, yp); if(check_retval(&retval, "IDAInit", 1)) return(1); /* Call IDASVtolerances to set tolerances */ retval = IDASVtolerances(mem, rtol, avtol); if(check_retval(&retval, "IDASVtolerances", 1)) return(1); /* Free avtol */ N_VDestroy(avtol); /* Call IDARootInit to specify the root function grob with 2 components */ retval = IDARootInit(mem, 2, grob); if (check_retval(&retval, "IDARootInit", 1)) return(1); /* Create sparse SUNMatrix for use in linear solves */ nnz = NEQ * NEQ; A = SUNSparseMatrix(NEQ, NEQ, nnz, CSC_MAT); if(check_retval((void *)A, "SUNSparseMatrix", 0)) return(1); /* Create SuperLUMT SUNLinearSolver object (one thread) */ LS = SUNLinSol_SuperLUMT(yy, A, 1); if(check_retval((void *)LS, "SUNLinSol_SuperLUMT", 0)) return(1); /* Attach the matrix and linear solver */ retval = IDASetLinearSolver(mem, LS, A); if(check_retval(&retval, "IDASetLinearSolver", 1)) return(1); /* Set the user-supplied Jacobian routine */ retval = IDASetJacFn(mem, jacrob); if(check_retval(&retval, "IDASetJacFn", 1)) return(1); /* In loop, call IDASolve, print results, and test for error. Break out of loop when NOUT preset output times have been reached. */ iout = 0; tout = tout1; while(1) { retval = IDASolve(mem, tout, &tret, yy, yp, IDA_NORMAL); PrintOutput(mem,tret,yy); if(check_retval(&retval, "IDASolve", 1)) return(1); if (retval == IDA_ROOT_RETURN) { retvalr = IDAGetRootInfo(mem, rootsfound); check_retval(&retvalr, "IDAGetRootInfo", 1); PrintRootInfo(rootsfound[0],rootsfound[1]); } if (retval == IDA_SUCCESS) { iout++; tout *= RCONST(10.0); } if (iout == NOUT) break; } PrintFinalStats(mem); /* Free memory */ IDAFree(&mem); SUNLinSolFree(LS); SUNMatDestroy(A); N_VDestroy(yy); N_VDestroy(yp); return(0); }
int main(void) { int globalstrategy; realtype fnormtol, scsteptol; N_Vector cc, sc, constraints; UserData data; int flag, maxl, maxlrst; void *kmem; cc = sc = constraints = NULL; kmem = NULL; data = NULL; /* Allocate memory, and set problem data, initial values, tolerances */ globalstrategy = KIN_NONE; data = AllocUserData(); if (check_flag((void *)data, "AllocUserData", 2)) return(1); InitUserData(data); /* Create serial vectors of length NEQ */ cc = N_VNew_Serial(NEQ); if (check_flag((void *)cc, "N_VNew_Serial", 0)) return(1); sc = N_VNew_Serial(NEQ); if (check_flag((void *)sc, "N_VNew_Serial", 0)) return(1); data->rates = N_VNew_Serial(NEQ); if (check_flag((void *)data->rates, "N_VNew_Serial", 0)) return(1); constraints = N_VNew_Serial(NEQ); if (check_flag((void *)constraints, "N_VNew_Serial", 0)) return(1); N_VConst(TWO, constraints); SetInitialProfiles(cc, sc); fnormtol=FTOL; scsteptol=STOL; /* Call KINCreate/KINInit to initialize KINSOL. A pointer to KINSOL problem memory is returned and stored in kmem. */ kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0)) return(1); /* Vector cc passed as template vector. */ flag = KINInit(kmem, func, cc); if (check_flag(&flag, "KINInit", 1)) return(1); flag = KINSetUserData(kmem, data); if (check_flag(&flag, "KINSetUserData", 1)) return(1); flag = KINSetConstraints(kmem, constraints); if (check_flag(&flag, "KINSetConstraints", 1)) return(1); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1); /* We no longer need the constraints vector since KINSetConstraints creates a private copy for KINSOL to use. */ N_VDestroy_Serial(constraints); /* Call KINSpgmr to specify the linear solver KINSPGMR with preconditioner routines PrecSetupBD and PrecSolveBD. */ maxl = 15; maxlrst = 2; flag = KINSpgmr(kmem, maxl); if (check_flag(&flag, "KINSpgmr", 1)) return(1); flag = KINSpilsSetMaxRestarts(kmem, maxlrst); if (check_flag(&flag, "KINSpilsSetMaxRestarts", 1)) return(1); flag = KINSpilsSetPreconditioner(kmem, PrecSetupBD, PrecSolveBD); if (check_flag(&flag, "KINSpilsSetPreconditioner", 1)) return(1); /* Print out the problem size, solution parameters, initial guess. */ PrintHeader(globalstrategy, maxl, maxlrst, fnormtol, scsteptol); /* Call KINSol and print output concentration profile */ flag = KINSol(kmem, /* KINSol memory block */ cc, /* initial guess on input; solution vector */ globalstrategy, /* global stragegy choice */ sc, /* scaling vector, for the variable cc */ sc); /* scaling vector for function values fval */ if (check_flag(&flag, "KINSol", 1)) return(1); printf("\n\nComputed equilibrium species concentrations:\n"); PrintOutput(cc); /* Print final statistics and free memory */ PrintFinalStats(kmem); N_VDestroy_Serial(cc); N_VDestroy_Serial(sc); KINFree(&kmem); FreeUserData(data); return(0); }
int main(int argc, char *argv[]) { void *cvode_mem; UserData data; realtype abstol, reltol, t, tout; N_Vector y; int iout, flag; realtype *pbar; int is, *plist; N_Vector *uS; booleantype sensi, err_con; int sensi_meth; pbar = NULL; plist = NULL; uS = NULL; y = NULL; data = NULL; cvode_mem = NULL; /* Process arguments */ ProcessArgs(argc, argv, &sensi, &sensi_meth, &err_con); /* Problem parameters */ data = AllocUserData(); if(check_flag((void *)data, "AllocUserData", 2)) return(1); InitUserData(data); /* Initial states */ y = N_VNew_Serial(NEQ); if(check_flag((void *)y, "N_VNew_Serial", 0)) return(1); SetInitialProfiles(y, data->dx, data->dz); /* Tolerances */ abstol=ATOL; reltol=RTOL; /* Create CVODES object */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); flag = CVodeSetFdata(cvode_mem, data); if(check_flag(&flag, "CVodeSetFdata", 1)) return(1); flag = CVodeSetMaxNumSteps(cvode_mem, 2000); if(check_flag(&flag, "CVodeSetMaxNumSteps", 1)) return(1); /* Allocate CVODES memory */ flag = CVodeMalloc(cvode_mem, f, T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); /* Attach CVSPGMR linear solver */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); flag = CVSpilsSetPreconditioner(cvode_mem, Precond, PSolve, data); if(check_flag(&flag, "CVSpilsSetPreconditioner", 1)) return(1); printf("\n2-species diurnal advection-diffusion problem\n"); /* Forward sensitivity analysis */ if(sensi) { plist = (int *) malloc(NS * sizeof(int)); if(check_flag((void *)plist, "malloc", 2)) return(1); for(is=0; is<NS; is++) plist[is] = is; pbar = (realtype *) malloc(NS * sizeof(realtype)); if(check_flag((void *)pbar, "malloc", 2)) return(1); for(is=0; is<NS; is++) pbar[is] = data->p[plist[is]]; uS = N_VCloneVectorArray_Serial(NS, y); if(check_flag((void *)uS, "N_VCloneVectorArray_Serial", 0)) return(1); for(is=0;is<NS;is++) N_VConst(ZERO,uS[is]); flag = CVodeSensMalloc(cvode_mem, NS, sensi_meth, uS); if(check_flag(&flag, "CVodeSensMalloc", 1)) return(1); flag = CVodeSetSensErrCon(cvode_mem, err_con); if(check_flag(&flag, "CVodeSetSensErrCon", 1)) return(1); flag = CVodeSetSensRho(cvode_mem, ZERO); if(check_flag(&flag, "CVodeSetSensRho", 1)) return(1); flag = CVodeSetSensParams(cvode_mem, data->p, pbar, plist); if(check_flag(&flag, "CVodeSetSensParams", 1)) return(1); printf("Sensitivity: YES "); if(sensi_meth == CV_SIMULTANEOUS) printf("( SIMULTANEOUS +"); else if(sensi_meth == CV_STAGGERED) printf("( STAGGERED +"); else printf("( STAGGERED1 +"); if(err_con) printf(" FULL ERROR CONTROL )"); else printf(" PARTIAL ERROR CONTROL )"); } else { printf("Sensitivity: NO "); } /* In loop over output points, call CVode, print results, test for error */ printf("\n\n"); printf("========================================================================\n"); printf(" T Q H NST Bottom left Top right \n"); printf("========================================================================\n"); for (iout=1, tout = TWOHR; iout <= NOUT; iout++, tout += TWOHR) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); if(check_flag(&flag, "CVode", 1)) break; PrintOutput(cvode_mem, t, y); if (sensi) { flag = CVodeGetSens(cvode_mem, t, uS); if(check_flag(&flag, "CVodeGetSens", 1)) break; PrintOutputS(uS); } printf("------------------------------------------------------------------------\n"); } /* Print final statistics */ PrintFinalStats(cvode_mem, sensi); /* Free memory */ N_VDestroy_Serial(y); if (sensi) { N_VDestroyVectorArray_Serial(uS, NS); free(pbar); free(plist); } FreeUserData(data); CVodeFree(&cvode_mem); return(0); }
int main() { UserData data; realtype fnormtol, scsteptol; N_Vector u1, u2, u, s, c; int glstr, mset, flag; void *kmem; u1 = u2 = u = NULL; s = c = NULL; kmem = NULL; data = NULL; /* User data */ data = (UserData)malloc(sizeof *data); data->lb[0] = PT25; data->ub[0] = ONE; data->lb[1] = ONEPT5; data->ub[1] = TWO*PI; /* Create serial vectors of length NEQ */ u1 = N_VNew_Serial(NEQ); if (check_flag((void *)u1, "N_VNew_Serial", 0)) return(1); u2 = N_VNew_Serial(NEQ); if (check_flag((void *)u2, "N_VNew_Serial", 0)) return(1); u = N_VNew_Serial(NEQ); if (check_flag((void *)u, "N_VNew_Serial", 0)) return(1); s = N_VNew_Serial(NEQ); if (check_flag((void *)s, "N_VNew_Serial", 0)) return(1); c = N_VNew_Serial(NEQ); if (check_flag((void *)c, "N_VNew_Serial", 0)) return(1); SetInitialGuess1(u1,data); SetInitialGuess2(u2,data); N_VConst_Serial(ONE,s); /* no scaling */ Ith(c,1) = ZERO; /* no constraint on x1 */ Ith(c,2) = ZERO; /* no constraint on x2 */ Ith(c,3) = ONE; /* l1 = x1 - x1_min >= 0 */ Ith(c,4) = -ONE; /* L1 = x1 - x1_max <= 0 */ Ith(c,5) = ONE; /* l2 = x2 - x2_min >= 0 */ Ith(c,6) = -ONE; /* L2 = x2 - x22_min <= 0 */ fnormtol=FTOL; scsteptol=STOL; kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0)) return(1); flag = KINSetUserData(kmem, data); if (check_flag(&flag, "KINSetUserData", 1)) return(1); flag = KINSetConstraints(kmem, c); if (check_flag(&flag, "KINSetConstraints", 1)) return(1); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1); flag = KINInit(kmem, func, u); if (check_flag(&flag, "KINInit", 1)) return(1); /* Call KINDense to specify the linear solver */ flag = KINDense(kmem, NEQ); if (check_flag(&flag, "KINDense", 1)) return(1); /* Print out the problem size, solution parameters, initial guess. */ PrintHeader(fnormtol, scsteptol); /* --------------------------- */ printf("\n------------------------------------------\n"); printf("\nInitial guess on lower bounds\n"); printf(" [x1,x2] = "); PrintOutput(u1); N_VScale_Serial(ONE,u1,u); glstr = KIN_NONE; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_LINESEARCH; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_NONE; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_LINESEARCH; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ printf("\n------------------------------------------\n"); printf("\nInitial guess in middle of feasible region\n"); printf(" [x1,x2] = "); PrintOutput(u2); N_VScale_Serial(ONE,u2,u); glstr = KIN_NONE; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_LINESEARCH; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_NONE; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_LINESEARCH; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* Free memory */ N_VDestroy_Serial(u); N_VDestroy_Serial(s); N_VDestroy_Serial(c); KINFree(&kmem); free(data); return(0); }
int main() { realtype t, tout; N_Vector y; void *cvode_mem; int flag, flagr, iout; int rootsfound[2]; y = NULL; cvode_mem = NULL; /* Create serial vector of length NEQ for I.C. */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); /* Initialize y */ Ith(y,1) = Y1; Ith(y,2) = Y2; Ith(y,3) = Y3; /* Call CVodeCreate to create the solver memory and specify the * Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in y'=f(t,y), the inital time T0, and * the initial dependent variable vector y. */ flag = CVodeInit(cvode_mem, f, T0, y); if (check_flag(&flag, "CVodeInit", 1)) return(1); /* Use private function to compute error weights */ flag = CVodeWFtolerances(cvode_mem, ewt); if (check_flag(&flag, "CVodeSetEwtFn", 1)) return(1); /* Call CVodeRootInit to specify the root function g with 2 components */ flag = CVodeRootInit(cvode_mem, 2, g); if (check_flag(&flag, "CVodeRootInit", 1)) return(1); /* Call CVDense to specify the CVDENSE dense linear solver */ flag = CVDense(cvode_mem, NEQ); if (check_flag(&flag, "CVDense", 1)) return(1); /* Set the Jacobian routine to Jac (user-supplied) */ flag = CVDlsSetDenseJacFn(cvode_mem, Jac); if (check_flag(&flag, "CVDlsSetDenseJacFn", 1)) return(1); /* In loop, call CVode, print results, and test for error. Break out of loop when NOUT preset output times have been reached. */ printf(" \n3-species kinetics problem\n\n"); iout = 0; tout = T1; while(1) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); PrintOutput(t, Ith(y,1), Ith(y,2), Ith(y,3)); if (flag == CV_ROOT_RETURN) { flagr = CVodeGetRootInfo(cvode_mem, rootsfound); check_flag(&flagr, "CVodeGetRootInfo", 1); PrintRootInfo(rootsfound[0],rootsfound[1]); } if (check_flag(&flag, "CVode", 1)) break; if (flag == CV_SUCCESS) { iout++; tout *= TMULT; } if (iout == NOUT) break; } /* Print some final statistics */ PrintFinalStats(cvode_mem); /* Free y vector */ N_VDestroy_Serial(y); /* Free integrator memory */ CVodeFree(&cvode_mem); return(0); }
int main(int narg, char **args) { realtype reltol, t, tout; N_Vector state, abstol; void *cvode_mem; int flag, flagr; int rootsfound[NRF]; int rootdir[] = {1,}; FILE *pout; if(!(pout = fopen("results/iaf_v.dat", "w"))){ fprintf(stderr, "Cannot open file results/iaf_v.dat. Are you trying to write to a non-existent directory? Exiting...\n"); exit(1); } state = abstol = NULL; cvode_mem = NULL; state = N_VNew_Serial(NEQ); if (check_flag((void *)state, "N_VNew_Serial", 0)) return(1); abstol = N_VNew_Serial(NEQ); if (check_flag((void *)abstol, "N_VNew_Serial", 0)) return(1); realtype reset = -0.07; realtype C = 3.2e-12; realtype thresh = -0.055; realtype gleak = 2e-10; realtype eleak = -0.053; realtype p[] = {reset, C, thresh, gleak, eleak, }; realtype v = reset; NV_Ith_S(state, 0) = reset; reltol = RTOL; NV_Ith_S(abstol,0) = ATOL0; /* Allocations and initializations */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); flag = CVodeInit(cvode_mem, dstate_dt, T0, state); if (check_flag(&flag, "CVodeInit", 1)) return(1); flag = CVodeSetUserData(cvode_mem, p); if (check_flag(&flag, "CVodeSetUserData", 1)) return(1); flag = CVodeSVtolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSVtolerances", 1)) return(1); flag = CVodeRootInit(cvode_mem, NRF, root_functions); if (check_flag(&flag, "CVodeRootInit", 1)) return(1); CVodeSetRootDirection(cvode_mem, rootdir); if (check_flag(&flag, "CVodeSetRootDirection", 1)) return(1); flag = CVDense(cvode_mem, NEQ); if (check_flag(&flag, "CVDense", 1)) return(1); printf(" \n Integrating iaf \n\n"); printf("#t v, \n"); PrintOutput(pout, t, state); tout = DT; while(1) { flag = CVode(cvode_mem, tout, state, &t, CV_NORMAL); if(flag == CV_ROOT_RETURN) { /* Event detected */ flagr = CVodeGetRootInfo(cvode_mem, rootsfound); if (check_flag(&flagr, "CVodeGetRootInfo", 1)) return(1); PrintRootInfo(t, state, rootsfound); if(rootsfound[0]){ //condition_0 v = NV_Ith_S(state, 0); NV_Ith_S(state, 0) = reset; } /* Restart integration with event-corrected state */ flag = CVodeSetUserData(cvode_mem, p); if (check_flag(&flag, "CVodeSetUserData", 1)) return(1); CVodeReInit(cvode_mem, t, state); //PrintRootInfo(t, state, rootsfound); } else { PrintOutput(pout, t, state); if(check_flag(&flag, "CVode", 1)) break; if(flag == CV_SUCCESS) { tout += DT; } if (t >= T1) break; } } PrintFinalStats(cvode_mem); N_VDestroy_Serial(state); N_VDestroy_Serial(abstol); CVodeFree(&cvode_mem); fclose(pout); return(0); }
int main() { realtype fnormtol, scsteptol; N_Vector y, scale, constraints; int mset, flag, i; void *kmem; SUNMatrix J; SUNLinearSolver LS; y = scale = constraints = NULL; kmem = NULL; J = NULL; LS = NULL; printf("\nRobot Kinematics Example\n"); printf("8 variables; -1 <= x_i <= 1\n"); printf("KINSOL problem size: 8 + 2*8 = 24 \n\n"); /* Create vectors for solution, scales, and constraints */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); scale = N_VNew_Serial(NEQ); if (check_flag((void *)scale, "N_VNew_Serial", 0)) return(1); constraints = N_VNew_Serial(NEQ); if (check_flag((void *)constraints, "N_VNew_Serial", 0)) return(1); /* Initialize and allocate memory for KINSOL */ kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0)) return(1); flag = KINInit(kmem, func, y); /* y passed as a template */ if (check_flag(&flag, "KINInit", 1)) return(1); /* Set optional inputs */ N_VConst_Serial(ZERO,constraints); for (i = NVAR+1; i <= NEQ; i++) Ith(constraints, i) = ONE; flag = KINSetConstraints(kmem, constraints); if (check_flag(&flag, "KINSetConstraints", 1)) return(1); fnormtol = FTOL; flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1); scsteptol = STOL; flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1); /* Create dense SUNMatrix */ J = SUNDenseMatrix(NEQ, NEQ); if(check_flag((void *)J, "SUNDenseMatrix", 0)) return(1); /* Create dense SUNLinearSolver object */ LS = SUNLinSol_Dense(y, J); if(check_flag((void *)LS, "SUNLinSol_Dense", 0)) return(1); /* Attach the matrix and linear solver to KINSOL */ flag = KINSetLinearSolver(kmem, LS, J); if(check_flag(&flag, "KINSetLinearSolver", 1)) return(1); /* Set the Jacobian function */ flag = KINSetJacFn(kmem, jac); if (check_flag(&flag, "KINSetJacFn", 1)) return(1); /* Indicate exact Newton */ mset = 1; flag = KINSetMaxSetupCalls(kmem, mset); if (check_flag(&flag, "KINSetMaxSetupCalls", 1)) return(1); /* Initial guess */ N_VConst_Serial(ONE, y); for(i = 1; i <= NVAR; i++) Ith(y,i) = SUNRsqrt(TWO)/TWO; printf("Initial guess:\n"); PrintOutput(y); /* Call KINSol to solve problem */ N_VConst_Serial(ONE,scale); flag = KINSol(kmem, /* KINSol memory block */ y, /* initial guess on input; solution vector */ KIN_LINESEARCH, /* global strategy choice */ scale, /* scaling vector, for the variable cc */ scale); /* scaling vector for function values fval */ if (check_flag(&flag, "KINSol", 1)) return(1); printf("\nComputed solution:\n"); PrintOutput(y); /* Print final statistics and free memory */ PrintFinalStats(kmem); N_VDestroy_Serial(y); N_VDestroy_Serial(scale); N_VDestroy_Serial(constraints); KINFree(&kmem); SUNLinSolFree(LS); SUNMatDestroy(J); return(0); }
int main() { realtype abstol, reltol, t, tout; N_Vector u; UserData data; void *cvode_mem; int iout, flag; u = NULL; data = NULL; cvode_mem = NULL; /* Allocate memory, and set problem data, initial values, tolerances */ u = N_VNew_Serial(NEQ); if(check_flag((void *)u, "N_VNew_Serial", 0)) return(1); data = AllocUserData(); if(check_flag((void *)data, "AllocUserData", 2)) return(1); InitUserData(data); SetInitialProfiles(u, data->dx, data->dy); abstol=ATOL; reltol=RTOL; /* Call CvodeCreate to create the solver memory CV_BDF specifies the Backward Differentiation Formula CV_NEWTON specifies a Newton iteration A pointer to the integrator memory is returned and stored in cvode_mem. */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Set the pointer to user-defined data */ flag = CVodeSetFdata(cvode_mem, data); if(check_flag(&flag, "CVodeSetFdata", 1)) return(1); /* Call CVodeMalloc to initialize the integrator memory: f is the user's right hand side function in u'=f(t,u) T0 is the initial time u is the initial dependent variable vector CV_SS specifies scalar relative and absolute tolerances reltol is the relative tolerance &abstol is a pointer to the scalar absolute tolerance */ flag = CVodeMalloc(cvode_mem, f, T0, u, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); /* Call CVSpgmr to specify the linear solver CVSPGMR with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); /* Set modified Gram-Schmidt orthogonalization, preconditioner setup and solve routines Precond and PSolve, and the pointer to the user-defined block data */ flag = CVSpgmrSetGSType(cvode_mem, MODIFIED_GS); if(check_flag(&flag, "CVSpgmrSetGSType", 1)) return(1); flag = CVSpgmrSetPreconditioner(cvode_mem, Precond, PSolve, data); if(check_flag(&flag, "CVSpgmrSetPreconditioner", 1)) return(1); /* In loop over output points, call CVode, print results, test for error */ printf(" \n2-species diurnal advection-diffusion problem\n\n"); for (iout=1, tout = TWOHR; iout <= NOUT; iout++, tout += TWOHR) { flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL); PrintOutput(cvode_mem, u, t); if(check_flag(&flag, "CVode", 1)) break; } PrintFinalStats(cvode_mem); /* Free memory */ N_VDestroy_Serial(u); FreeUserData(data); CVodeFree(cvode_mem); return(0); }
int main(void) { void *mem; N_Vector yy, yp, avtol; realtype rtol, *yval, *ypval, *atval; realtype t0, tout1, tout, tret; int iout, retval, retvalr; int rootsfound[2]; int nnz; mem = NULL; yy = yp = avtol = NULL; yval = ypval = atval = NULL; /* Allocate N-vectors. */ yy = N_VNew_Serial(NEQ); if(check_flag((void *)yy, "N_VNew_Serial", 0)) return(1); yp = N_VNew_Serial(NEQ); if(check_flag((void *)yp, "N_VNew_Serial", 0)) return(1); avtol = N_VNew_Serial(NEQ); if(check_flag((void *)avtol, "N_VNew_Serial", 0)) return(1); /* Create and initialize y, y', and absolute tolerance vectors. */ yval = NV_DATA_S(yy); yval[0] = ONE; yval[1] = ZERO; yval[2] = ZERO; ypval = NV_DATA_S(yp); ypval[0] = RCONST(-0.04); ypval[1] = RCONST(0.04); ypval[2] = ZERO; rtol = RCONST(1.0e-4); atval = NV_DATA_S(avtol); atval[0] = RCONST(1.0e-8); atval[1] = RCONST(1.0e-6); atval[2] = RCONST(1.0e-6); /* Integration limits */ t0 = ZERO; tout1 = RCONST(0.4); PrintHeader(rtol, avtol, yy); /* Call IDACreate and IDAMalloc to initialize IDA memory */ mem = IDACreate(); if(check_flag((void *)mem, "IDACreate", 0)) return(1); retval = IDAInit(mem, resrob, t0, yy, yp); if(check_flag(&retval, "IDAInit", 1)) return(1); retval = IDASVtolerances(mem, rtol, avtol); if(check_flag(&retval, "IDASVtolerances", 1)) return(1); /* Free avtol */ N_VDestroy_Serial(avtol); /* Call IDARootInit to specify the root function grob with 2 components */ retval = IDARootInit(mem, 2, grob); if (check_flag(&retval, "IDARootInit", 1)) return(1); /* Call IDASuperLUMT and set up the linear solver. */ nnz = NEQ * NEQ; retval = IDASuperLUMT(mem, 1, NEQ, nnz); if(check_flag(&retval, "IDASuperLUMT", 1)) return(1); retval = IDASlsSetSparseJacFn(mem, jacrob); if(check_flag(&retval, "IDASlsSetSparseJacFn", 1)) return(1); /* In loop, call IDASolve, print results, and test for error. Break out of loop when NOUT preset output times have been reached. */ iout = 0; tout = tout1; while(1) { retval = IDASolve(mem, tout, &tret, yy, yp, IDA_NORMAL); PrintOutput(mem,tret,yy); if(check_flag(&retval, "IDASolve", 1)) return(1); if (retval == IDA_ROOT_RETURN) { retvalr = IDAGetRootInfo(mem, rootsfound); check_flag(&retvalr, "IDAGetRootInfo", 1); PrintRootInfo(rootsfound[0],rootsfound[1]); } if (retval == IDA_SUCCESS) { iout++; tout *= RCONST(10.0); } if (iout == NOUT) break; } PrintFinalStats(mem); /* Free memory */ IDAFree(&mem); N_VDestroy_Serial(yy); N_VDestroy_Serial(yp); return(0); }
int main(int argc, char *argv[]) { UserData data; SUNMatrix A, AB; SUNLinearSolver LS, LSB; void *cvode_mem; realtype reltolQ, abstolQ; N_Vector y, q, constraints; int steps; int indexB; realtype reltolB, abstolB, abstolQB; N_Vector yB, qB, constraintsB; realtype time; int retval, ncheck; long int nst, nstB; CVadjCheckPointRec *ckpnt; data = NULL; A = AB = NULL; LS = LSB = NULL; cvode_mem = NULL; ckpnt = NULL; y = yB = qB = NULL; constraints = NULL; constraintsB = NULL; /* Print problem description */ printf("\nAdjoint Sensitivity Example for Chemical Kinetics\n"); printf("-------------------------------------------------\n\n"); printf("ODE: dy1/dt = -p1*y1 + p2*y2*y3\n"); printf(" dy2/dt = p1*y1 - p2*y2*y3 - p3*(y2)^2\n"); printf(" dy3/dt = p3*(y2)^2\n\n"); printf("Find dG/dp for\n"); printf(" G = int_t0^tB0 g(t,p,y) dt\n"); printf(" g(t,p,y) = y3\n\n\n"); /* User data structure */ data = (UserData) malloc(sizeof *data); if (check_retval((void *)data, "malloc", 2)) return(1); data->p[0] = RCONST(0.04); data->p[1] = RCONST(1.0e4); data->p[2] = RCONST(3.0e7); /* Initialize y */ y = N_VNew_Serial(NEQ); if (check_retval((void *)y, "N_VNew_Serial", 0)) return(1); Ith(y,1) = RCONST(1.0); Ith(y,2) = ZERO; Ith(y,3) = ZERO; /* Set constraints to all 1's for nonnegative solution values. */ constraints = N_VNew_Serial(NEQ); if(check_retval((void *)constraints, "N_VNew_Serial", 0)) return(1); N_VConst(ONE, constraints); /* Initialize q */ q = N_VNew_Serial(1); if (check_retval((void *)q, "N_VNew_Serial", 0)) return(1); Ith(q,1) = ZERO; /* Set the scalar realtive and absolute tolerances reltolQ and abstolQ */ reltolQ = RTOL; abstolQ = ATOLq; /* Create and allocate CVODES memory for forward run */ printf("Create and allocate CVODES memory for forward runs\n"); /* Call CVodeCreate to create the solver memory and specify the Backward Differentiation Formula */ cvode_mem = CVodeCreate(CV_BDF); if (check_retval((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the user's right hand side function in y'=f(t,y), the initial time T0, and the initial dependent variable vector y. */ retval = CVodeInit(cvode_mem, f, T0, y); if (check_retval(&retval, "CVodeInit", 1)) return(1); /* Call CVodeWFtolerances to specify a user-supplied function ewt that sets the multiplicative error weights w_i for use in the weighted RMS norm */ retval = CVodeWFtolerances(cvode_mem, ewt); if (check_retval(&retval, "CVodeWFtolerances", 1)) return(1); /* Attach user data */ retval = CVodeSetUserData(cvode_mem, data); if (check_retval(&retval, "CVodeSetUserData", 1)) return(1); /* Call CVodeSetConstraints to initialize constraints */ retval = CVodeSetConstraints(cvode_mem, constraints); if (check_retval(&retval, "CVODESetConstraints", 1)) return(1); N_VDestroy(constraints); /* Create dense SUNMatrix for use in linear solves */ A = SUNDenseMatrix(NEQ, NEQ); if (check_retval((void *)A, "SUNDenseMatrix", 0)) return(1); /* Create dense SUNLinearSolver object */ LS = SUNLinSol_Dense(y, A); if (check_retval((void *)LS, "SUNLinSol_Dense", 0)) return(1); /* Attach the matrix and linear solver */ retval = CVDlsSetLinearSolver(cvode_mem, LS, A); if (check_retval(&retval, "CVDlsSetLinearSolver", 1)) return(1); /* Set the user-supplied Jacobian routine Jac */ retval = CVDlsSetJacFn(cvode_mem, Jac); if (check_retval(&retval, "CVDlsSetJacFn", 1)) return(1); /* Call CVodeQuadInit to allocate initernal memory and initialize quadrature integration*/ retval = CVodeQuadInit(cvode_mem, fQ, q); if (check_retval(&retval, "CVodeQuadInit", 1)) return(1); /* Call CVodeSetQuadErrCon to specify whether or not the quadrature variables are to be used in the step size control mechanism within CVODES. Call CVodeQuadSStolerances or CVodeQuadSVtolerances to specify the integration tolerances for the quadrature variables. */ retval = CVodeSetQuadErrCon(cvode_mem, SUNTRUE); if (check_retval(&retval, "CVodeSetQuadErrCon", 1)) return(1); /* Call CVodeQuadSStolerances to specify scalar relative and absolute tolerances. */ retval = CVodeQuadSStolerances(cvode_mem, reltolQ, abstolQ); if (check_retval(&retval, "CVodeQuadSStolerances", 1)) return(1); /* Allocate global memory */ /* Call CVodeAdjInit to update CVODES memory block by allocting the internal memory needed for backward integration.*/ steps = STEPS; /* no. of integration steps between two consecutive ckeckpoints*/ retval = CVodeAdjInit(cvode_mem, steps, CV_HERMITE); /* retval = CVodeAdjInit(cvode_mem, steps, CV_POLYNOMIAL); */ if (check_retval(&retval, "CVodeAdjInit", 1)) return(1); /* Perform forward run */ printf("Forward integration ... "); /* Call CVodeF to integrate the forward problem over an interval in time and saves checkpointing data */ retval = CVodeF(cvode_mem, TOUT, y, &time, CV_NORMAL, &ncheck); if (check_retval(&retval, "CVodeF", 1)) return(1); retval = CVodeGetNumSteps(cvode_mem, &nst); if (check_retval(&retval, "CVodeGetNumSteps", 1)) return(1); printf("done ( nst = %ld )\n",nst); printf("\nncheck = %d\n\n", ncheck); retval = CVodeGetQuad(cvode_mem, &time, q); if (check_retval(&retval, "CVodeGetQuad", 1)) return(1); printf("--------------------------------------------------------\n"); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("G: %12.4Le \n",Ith(q,1)); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("G: %12.4e \n",Ith(q,1)); #else printf("G: %12.4e \n",Ith(q,1)); #endif printf("--------------------------------------------------------\n\n"); /* Test check point linked list (uncomment next block to print check point information) */ /* { int i; printf("\nList of Check Points (ncheck = %d)\n\n", ncheck); ckpnt = (CVadjCheckPointRec *) malloc ( (ncheck+1)*sizeof(CVadjCheckPointRec)); CVodeGetAdjCheckPointsInfo(cvode_mem, ckpnt); for (i=0;i<=ncheck;i++) { printf("Address: %p\n",ckpnt[i].my_addr); printf("Next: %p\n",ckpnt[i].next_addr); printf("Time interval: %le %le\n",ckpnt[i].t0, ckpnt[i].t1); printf("Step number: %ld\n",ckpnt[i].nstep); printf("Order: %d\n",ckpnt[i].order); printf("Step size: %le\n",ckpnt[i].step); printf("\n"); } } */ /* Initialize yB */ yB = N_VNew_Serial(NEQ); if (check_retval((void *)yB, "N_VNew_Serial", 0)) return(1); Ith(yB,1) = ZERO; Ith(yB,2) = ZERO; Ith(yB,3) = ZERO; /* Initialize qB */ qB = N_VNew_Serial(NP); if (check_retval((void *)qB, "N_VNew", 0)) return(1); Ith(qB,1) = ZERO; Ith(qB,2) = ZERO; Ith(qB,3) = ZERO; /* Set the scalar relative tolerance reltolB */ reltolB = RTOL; /* Set the scalar absolute tolerance abstolB */ abstolB = ATOLl; /* Set the scalar absolute tolerance abstolQB */ abstolQB = ATOLq; /* Set constraints to all 1's for nonnegative solution values. */ constraintsB = N_VNew_Serial(NEQ); if(check_retval((void *)constraintsB, "N_VNew_Serial", 0)) return(1); N_VConst(ONE, constraintsB); /* Create and allocate CVODES memory for backward run */ printf("Create and allocate CVODES memory for backward run\n"); /* Call CVodeCreateB to specify the solution method for the backward problem. */ retval = CVodeCreateB(cvode_mem, CV_BDF, &indexB); if (check_retval(&retval, "CVodeCreateB", 1)) return(1); /* Call CVodeInitB to allocate internal memory and initialize the backward problem. */ retval = CVodeInitB(cvode_mem, indexB, fB, TB1, yB); if (check_retval(&retval, "CVodeInitB", 1)) return(1); /* Set the scalar relative and absolute tolerances. */ retval = CVodeSStolerancesB(cvode_mem, indexB, reltolB, abstolB); if (check_retval(&retval, "CVodeSStolerancesB", 1)) return(1); /* Attach the user data for backward problem. */ retval = CVodeSetUserDataB(cvode_mem, indexB, data); if (check_retval(&retval, "CVodeSetUserDataB", 1)) return(1); /* Call CVodeSetConstraintsB to initialize constraints */ retval = CVodeSetConstraintsB(cvode_mem, indexB, constraintsB); if(check_retval(&retval, "CVodeSetConstraintsB", 1)) return(1); N_VDestroy(constraintsB); /* Create dense SUNMatrix for use in linear solves */ AB = SUNDenseMatrix(NEQ, NEQ); if (check_retval((void *)AB, "SUNDenseMatrix", 0)) return(1); /* Create dense SUNLinearSolver object */ LSB = SUNLinSol_Dense(yB, AB); if (check_retval((void *)LSB, "SUNLinSol_Dense", 0)) return(1); /* Attach the matrix and linear solver */ retval = CVDlsSetLinearSolverB(cvode_mem, indexB, LSB, AB); if (check_retval(&retval, "CVDlsSetLinearSolverB", 1)) return(1); /* Set the user-supplied Jacobian routine JacB */ retval = CVDlsSetJacFnB(cvode_mem, indexB, JacB); if (check_retval(&retval, "CVDlsSetJacFnB", 1)) return(1); /* Call CVodeQuadInitB to allocate internal memory and initialize backward quadrature integration. */ retval = CVodeQuadInitB(cvode_mem, indexB, fQB, qB); if (check_retval(&retval, "CVodeQuadInitB", 1)) return(1); /* Call CVodeSetQuadErrCon to specify whether or not the quadrature variables are to be used in the step size control mechanism within CVODES. Call CVodeQuadSStolerances or CVodeQuadSVtolerances to specify the integration tolerances for the quadrature variables. */ retval = CVodeSetQuadErrConB(cvode_mem, indexB, SUNTRUE); if (check_retval(&retval, "CVodeSetQuadErrConB", 1)) return(1); /* Call CVodeQuadSStolerancesB to specify the scalar relative and absolute tolerances for the backward problem. */ retval = CVodeQuadSStolerancesB(cvode_mem, indexB, reltolB, abstolQB); if (check_retval(&retval, "CVodeQuadSStolerancesB", 1)) return(1); /* Backward Integration */ PrintHead(TB1); /* First get results at t = TBout1 */ /* Call CVodeB to integrate the backward ODE problem. */ retval = CVodeB(cvode_mem, TBout1, CV_NORMAL); if (check_retval(&retval, "CVodeB", 1)) return(1); /* Call CVodeGetB to get yB of the backward ODE problem. */ retval = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_retval(&retval, "CVodeGetB", 1)) return(1); /* Call CVodeGetAdjY to get the interpolated value of the forward solution y during a backward integration. */ retval = CVodeGetAdjY(cvode_mem, TBout1, y); if (check_retval(&retval, "CVodeGetAdjY", 1)) return(1); PrintOutput1(time, TBout1, y, yB); /* Then at t = T0 */ retval = CVodeB(cvode_mem, T0, CV_NORMAL); if (check_retval(&retval, "CVodeB", 1)) return(1); CVodeGetNumSteps(CVodeGetAdjCVodeBmem(cvode_mem, indexB), &nstB); printf("Done ( nst = %ld )\n", nstB); retval = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_retval(&retval, "CVodeGetB", 1)) return(1); /* Call CVodeGetQuadB to get the quadrature solution vector after a successful return from CVodeB. */ retval = CVodeGetQuadB(cvode_mem, indexB, &time, qB); if (check_retval(&retval, "CVodeGetQuadB", 1)) return(1); retval = CVodeGetAdjY(cvode_mem, T0, y); if (check_retval(&retval, "CVodeGetAdjY", 1)) return(1); PrintOutput(time, y, yB, qB); /* Reinitialize backward phase (new tB0) */ Ith(yB,1) = ZERO; Ith(yB,2) = ZERO; Ith(yB,3) = ZERO; Ith(qB,1) = ZERO; Ith(qB,2) = ZERO; Ith(qB,3) = ZERO; printf("Re-initialize CVODES memory for backward run\n"); retval = CVodeReInitB(cvode_mem, indexB, TB2, yB); if (check_retval(&retval, "CVodeReInitB", 1)) return(1); retval = CVodeQuadReInitB(cvode_mem, indexB, qB); if (check_retval(&retval, "CVodeQuadReInitB", 1)) return(1); PrintHead(TB2); /* First get results at t = TBout1 */ retval = CVodeB(cvode_mem, TBout1, CV_NORMAL); if (check_retval(&retval, "CVodeB", 1)) return(1); retval = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_retval(&retval, "CVodeGetB", 1)) return(1); retval = CVodeGetAdjY(cvode_mem, TBout1, y); if (check_retval(&retval, "CVodeGetAdjY", 1)) return(1); PrintOutput1(time, TBout1, y, yB); /* Then at t = T0 */ retval = CVodeB(cvode_mem, T0, CV_NORMAL); if (check_retval(&retval, "CVodeB", 1)) return(1); CVodeGetNumSteps(CVodeGetAdjCVodeBmem(cvode_mem, indexB), &nstB); printf("Done ( nst = %ld )\n", nstB); retval = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_retval(&retval, "CVodeGetB", 1)) return(1); retval = CVodeGetQuadB(cvode_mem, indexB, &time, qB); if (check_retval(&retval, "CVodeGetQuadB", 1)) return(1); retval = CVodeGetAdjY(cvode_mem, T0, y); if (check_retval(&retval, "CVodeGetAdjY", 1)) return(1); PrintOutput(time, y, yB, qB); /* Free memory */ printf("Free memory\n\n"); CVodeFree(&cvode_mem); N_VDestroy(y); N_VDestroy(q); N_VDestroy(yB); N_VDestroy(qB); SUNLinSolFree(LS); SUNMatDestroy(A); SUNLinSolFree(LSB); SUNMatDestroy(AB); if (ckpnt != NULL) free(ckpnt); free(data); return(0); }
int main(int argc, char *argv[]) { MPI_Comm comm; void *mem; UserData data; int thispe, iout, ier, npes; long int Neq, local_N, mudq, mldq, mukeep, mlkeep; realtype rtol, atol, t0, t1, tout, tret; N_Vector uu, up, constraints, id, res; mem = NULL; data = NULL; uu = up = constraints = id = res = NULL; /* Get processor number and total number of pe's. */ MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &npes); MPI_Comm_rank(comm, &thispe); if (npes != NPEX*NPEY) { if (thispe == 0) fprintf(stderr, "\nMPI_ERROR(0): npes = %d is not equal to NPEX*NPEY = %d\n", npes,NPEX*NPEY); MPI_Finalize(); return(1); } /* Set local length local_N and global length Neq. */ local_N = MXSUB*MYSUB; Neq = MX * MY; /* Allocate N-vectors. */ uu = N_VNew_Parallel(comm, local_N, Neq); if(check_flag((void *)uu, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); up = N_VNew_Parallel(comm, local_N, Neq); if(check_flag((void *)up, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); res = N_VNew_Parallel(comm, local_N, Neq); if(check_flag((void *)res, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); constraints = N_VNew_Parallel(comm, local_N, Neq); if(check_flag((void *)constraints, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); id = N_VNew_Parallel(comm, local_N, Neq); if(check_flag((void *)id, "N_VNew_Parallel", 0, thispe)) MPI_Abort(comm, 1); /* Allocate and initialize the data structure. */ data = (UserData) malloc(sizeof *data); if(check_flag((void *)data, "malloc", 2, thispe)) MPI_Abort(comm, 1); InitUserData(thispe, comm, data); /* Initialize the uu, up, id, and constraints profiles. */ SetInitialProfile(uu, up, id, res, data); N_VConst(ONE, constraints); t0 = ZERO; t1 = RCONST(0.01); /* Scalar relative and absolute tolerance. */ rtol = ZERO; atol = RCONST(1.0e-3); /* Call IDACreate and IDAMalloc to initialize solution */ mem = IDACreate(); if(check_flag((void *)mem, "IDACreate", 0, thispe)) MPI_Abort(comm, 1); ier = IDASetUserData(mem, data); if(check_flag(&ier, "IDASetUserData", 1, thispe)) MPI_Abort(comm, 1); ier = IDASetSuppressAlg(mem, TRUE); if(check_flag(&ier, "IDASetSuppressAlg", 1, thispe)) MPI_Abort(comm, 1); ier = IDASetId(mem, id); if(check_flag(&ier, "IDASetId", 1, thispe)) MPI_Abort(comm, 1); ier = IDASetConstraints(mem, constraints); if(check_flag(&ier, "IDASetConstraints", 1, thispe)) MPI_Abort(comm, 1); N_VDestroy_Parallel(constraints); ier = IDAInit(mem, heatres, t0, uu, up); if(check_flag(&ier, "IDAInit", 1, thispe)) MPI_Abort(comm, 1); ier = IDASStolerances(mem, rtol, atol); if(check_flag(&ier, "IDASStolerances", 1, thispe)) MPI_Abort(comm, 1); mudq = MXSUB; mldq = MXSUB; mukeep = 1; mlkeep = 1; /* Print problem description */ if (thispe == 0 ) PrintHeader(Neq, rtol, atol); /* * ----------------------------- * Case 1 -- mldq = mudq = MXSUB * ----------------------------- */ /* Call IDASpgmr to specify the linear solver. */ ier = IDASpgmr(mem, 0); if(check_flag(&ier, "IDASpgmr", 1, thispe)) MPI_Abort(comm, 1); /* Call IDABBDPrecInit to initialize BBD preconditioner. */ ier = IDABBDPrecInit(mem, local_N, mudq, mldq, mukeep, mlkeep, ZERO, reslocal, NULL); if(check_flag(&ier, "IDABBDPrecAlloc", 1, thispe)) MPI_Abort(comm, 1); /* Print output heading (on processor 0 only) and initial solution. */ if (thispe == 0) PrintCase(1, mudq, mukeep); /* Loop over tout, call IDASolve, print output. */ for (tout = t1, iout = 1; iout <= NOUT; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1, thispe)) MPI_Abort(comm, 1); PrintOutput(thispe, mem, tret, uu); } /* Print final statistics */ if (thispe == 0) PrintFinalStats(mem); /* * ----------------------------- * Case 2 -- mldq = mudq = 1 * ----------------------------- */ mudq = 1; mldq = 1; /* Re-initialize the uu and up profiles. */ SetInitialProfile(uu, up, id, res, data); /* Call IDAReInit to re-initialize IDA. */ ier = IDAReInit(mem, t0, uu, up); if(check_flag(&ier, "IDAReInit", 1, thispe)) MPI_Abort(comm, 1); /* Call IDABBDPrecReInit to re-initialize BBD preconditioner. */ ier = IDABBDPrecReInit(mem, mudq, mldq, ZERO); if(check_flag(&ier, "IDABBDPrecReInit", 1, thispe)) MPI_Abort(comm, 1); /* Print output heading (on processor 0 only). */ if (thispe == 0) PrintCase(2, mudq, mukeep); /* Loop over tout, call IDASolve, print output. */ for (tout = t1, iout = 1; iout <= NOUT; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1, thispe)) MPI_Abort(comm, 1); PrintOutput(thispe, mem, tret, uu); } /* Print final statistics */ if (thispe == 0) PrintFinalStats(mem); /* Free Memory */ IDAFree(&mem); free(data); N_VDestroy_Parallel(id); N_VDestroy_Parallel(res); N_VDestroy_Parallel(up); N_VDestroy_Parallel(uu); MPI_Finalize(); return(0); }
int main(int argc, char *argv[]) { void *cvode_mem; UserData data; realtype t, tout; N_Vector y; int iout, flag, nthreads, nnz; realtype pbar[NS]; int is; N_Vector *yS; booleantype sensi, err_con; int sensi_meth; cvode_mem = NULL; data = NULL; y = NULL; yS = NULL; /* Process arguments */ ProcessArgs(argc, argv, &sensi, &sensi_meth, &err_con); /* User data structure */ data = (UserData) malloc(sizeof *data); if (check_flag((void *)data, "malloc", 2)) return(1); data->p[0] = RCONST(0.04); data->p[1] = RCONST(1.0e4); data->p[2] = RCONST(3.0e7); /* Initial conditions */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); Ith(y,1) = Y1; Ith(y,2) = Y2; Ith(y,3) = Y3; /* Call CVodeCreate to create the solver memory and specify the Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the user's right hand side function in y'=f(t,y), the initial time T0, and the initial dependent variable vector y. */ flag = CVodeInit(cvode_mem, f, T0, y); if (check_flag(&flag, "CVodeInit", 1)) return(1); /* Call CVodeWFtolerances to specify a user-supplied function ewt that sets the multiplicative error weights W_i for use in the weighted RMS norm */ flag = CVodeWFtolerances(cvode_mem, ewt); if (check_flag(&flag, "CVodeSetEwtFn", 1)) return(1); /* Attach user data */ flag = CVodeSetUserData(cvode_mem, data); if (check_flag(&flag, "CVodeSetUserData", 1)) return(1); /* Call CVKLU to specify the CVKLU sparse direct linear solver */ nthreads = 1; /* no. of threads to use when factoring the system*/ nnz = NEQ * NEQ; /* max no. of nonzeros entries in the Jac */ flag = CVSuperLUMT(cvode_mem, nthreads, NEQ, nnz); if (check_flag(&flag, "CVSuperLUMT", 1)) return(1); /* Set the Jacobian routine to Jac (user-supplied) */ flag = CVSlsSetSparseJacFn(cvode_mem, Jac); if (check_flag(&flag, "CVSlsSetSparseJacFn", 1)) return(1); printf("\n3-species chemical kinetics problem\n"); /* Sensitivity-related settings */ if (sensi) { /* Set parameter scaling factor */ pbar[0] = data->p[0]; pbar[1] = data->p[1]; pbar[2] = data->p[2]; /* Set sensitivity initial conditions */ yS = N_VCloneVectorArray_Serial(NS, y); if (check_flag((void *)yS, "N_VCloneVectorArray_Serial", 0)) return(1); for (is=0;is<NS;is++) N_VConst(ZERO, yS[is]); /* Call CVodeSensInit1 to activate forward sensitivity computations and allocate internal memory for COVEDS related to sensitivity calculations. Computes the right-hand sides of the sensitivity ODE, one at a time */ flag = CVodeSensInit1(cvode_mem, NS, sensi_meth, fS, yS); if(check_flag(&flag, "CVodeSensInit", 1)) return(1); /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity variables based on the rolerances supplied for states variables and the scaling factor pbar */ flag = CVodeSensEEtolerances(cvode_mem); if(check_flag(&flag, "CVodeSensEEtolerances", 1)) return(1); /* Set sensitivity analysis optional inputs */ /* Call CVodeSetSensErrCon to specify the error control strategy for sensitivity variables */ flag = CVodeSetSensErrCon(cvode_mem, err_con); if (check_flag(&flag, "CVodeSetSensErrCon", 1)) return(1); /* Call CVodeSetSensParams to specify problem parameter information for sensitivity calculations */ flag = CVodeSetSensParams(cvode_mem, NULL, pbar, NULL); if (check_flag(&flag, "CVodeSetSensParams", 1)) return(1); printf("Sensitivity: YES "); if(sensi_meth == CV_SIMULTANEOUS) printf("( SIMULTANEOUS +"); else if(sensi_meth == CV_STAGGERED) printf("( STAGGERED +"); else printf("( STAGGERED1 +"); if(err_con) printf(" FULL ERROR CONTROL )"); else printf(" PARTIAL ERROR CONTROL )"); } else { printf("Sensitivity: NO "); } /* In loop over output points, call CVode, print results, test for error */ printf("\n\n"); printf("==========================================="); printf("============================\n"); printf(" T Q H NST y1"); printf(" y2 y3 \n"); printf("==========================================="); printf("============================\n"); for (iout=1, tout=T1; iout <= NOUT; iout++, tout *= TMULT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); if (check_flag(&flag, "CVode", 1)) break; PrintOutput(cvode_mem, t, y); /* Call CVodeGetSens to get the sensitivity solution vector after a successful return from CVode */ if (sensi) { flag = CVodeGetSens(cvode_mem, &t, yS); if (check_flag(&flag, "CVodeGetSens", 1)) break; PrintOutputS(yS); } printf("-----------------------------------------"); printf("------------------------------\n"); } /* Print final statistics */ PrintFinalStats(cvode_mem, sensi); /* Free memory */ N_VDestroy_Serial(y); /* Free y vector */ if (sensi) { N_VDestroyVectorArray_Serial(yS, NS); /* Free yS vector */ } free(data); /* Free user data */ CVodeFree(&cvode_mem); /* Free CVODES memory */ return(0); }
int AUTO(AutoData *Data) { struct timeval *time0,*time1; integer icp[NPARX2]; doublereal par[NPARX2], thl[NPARX]; iap_type *iap; rap_type *rap; doublereal *thu; integer *iuz; doublereal *vuz; function_list list; integer i, j, k; // Initialize structures and constants gData = Data; iap = &(Data->iap); rap = &(Data->rap); Data->sp_len = Data->num_sp + (1 + floor(iap->nmx/iap->npr)); Data->sp_inc = 5; #ifdef USAGE struct rusage *init_usage,*total_usage; usage_start(&init_usage); usage_start(&total_usage); #endif #ifdef FLOATING_POINT_TRAP trapfpe(); #endif #ifdef PTHREADS global_conpar_type = CONPAR_PTHREADS; global_setubv_type = SETUBV_PTHREADS; global_reduce_type = REDUCE_PTHREADS; #endif fp9 = fopen("fort.9","w"); if(fp9 == NULL) { fprintf(stderr,"Error: Could not open fort.9\n"); exit(1); } /* Initialization : */ iap->mynode = mynode(); iap->numnodes = numnodes(); if (iap->numnodes > 1) { iap->parallel_flag = 1; } else { iap->parallel_flag = 0; } /* NOTE: thu is allocated inside this function, and the pointer is passed back. I know this is ugly, but this function does a bit of work to get thu setup correctly, as well as figuring out the size the array should be. What really should happen is to have one function which reads fort.2 and another fuction which initializes the array. That way the allocation could happen between the two calls. */ init0(iap, rap, par, icp, thl, &thu, &iuz, &vuz); /* Find restart label and determine type of restart point. */ if (iap->irs > 0) { logical found = FALSE_; findlb(iap, rap, iap->irs, &(iap->nfpr), &found); if (! found) { if (iap->mynode == 0) { fprintf(stderr,"\nRestart label %4ld not found\n",iap->irs); } exit(0); } } set_function_pointers(*iap,&list); init1(iap, rap, icp, par); chdim(iap); /* Create the allocations for the global structures used in autlib3.c and autlib5.c. These are purely an efficiency thing. The allocation and deallocation of these scratch areas takes up a nontrivial amount of time if done directly in the wrapper functions in autlib3.c*/ allocate_global_memory(*iap); /* ---------------------------------------------------------- */ /* ---------------------------------------------------------- */ /* One-parameter continuations */ /* ---------------------------------------------------------- */ /* ---------------------------------------------------------- */ #ifdef USAGE usage_end(init_usage,"main initialization"); #endif if (Data->print_input) PrintInput(Data, par, icp); // Initialize output variables if(list.type==AUTOAE) Data->u = DMATRIX(iap->nmx, iap->ndim); else { // Solution measures Data->usm = (doublereal ***)MALLOC((2+(int)(log2(Data->nsm)))*sizeof(doublereal **)); Data->usm[0] = DMATRIX(iap->nmx, iap->ndim); // MAX Data->usm[1] = DMATRIX(iap->nmx, iap->ndim); // MIN for (i=0; i<(int)(log2(Data->nsm)); i++) Data->usm[2+i] = DMATRIX(iap->nmx, iap->ndim); // Jacobian of flow if (Data->sjac) { Data->c0 = DMATRIX_3D(iap->nmx, iap->ndim, iap->ndim); Data->c1 = DMATRIX_3D(iap->nmx, iap->ndim, iap->ndim); } // Jacobian of flow along cycles (temporary storage) if (Data->sflow) { Data->a1 = DMATRIX_3D(iap->ntst, iap->ndim, iap->ndim); Data->a2 = DMATRIX_3D(iap->ntst, iap->ndim, iap->ndim); } } Data->par = DMATRIX(iap->nmx, iap->nicp); if (iap->isp >= 1) { Data->ev = DCMATRIX(iap->nmx, iap->ndim); for (i=0; i<iap->nmx; i++) { for (j=0; j<iap->ndim; j++) { Data->ev[i][j].r = NAN; // This is a flag for bad floquet multipliers Data->ev[i][j].i = NAN; } } } Data->num_u = 0; if (Data->sp == NULL) Data->num_sp = 0; Data->sp = (AutoSPData *)REALLOC(Data->sp, (Data->sp_len)*sizeof(AutoSPData)); for (i=Data->num_sp; i<Data->sp_len; i++) { Data->sp[i].u = NULL; Data->sp[i].icp = NULL; Data->sp[i].ups = NULL; Data->sp[i].udotps = NULL; Data->sp[i].rldot = NULL; Data->sp[i].a1 = NULL; Data->sp[i].a2 = NULL; } if(list.type==AUTOAE) autoae(iap, rap, par, icp, list.aelist.funi, list.aelist.stpnt, list.aelist.pvli, thl, thu, iuz, vuz); if(list.type==AUTOBV) autobv(iap, rap, par, icp, list.bvlist.funi, list.bvlist.bcni, list.bvlist.icni, list.bvlist.stpnt, list.bvlist.pvli, thl, thu, iuz, vuz); // Testing output if (Data->print_output) PrintOutput(Data); #ifdef USAGE usage_end(total_usage,"total"); #endif //time_end(time0,"Total Time ",fp9); fprintf(fp9,"----------------------------------------------"); fprintf(fp9,"----------------------------------------------\n"); //time_end(time1,"",stdout); //} FREE(thu); FREE(iuz); FREE(vuz); fclose(fp9); // Clean up special solution points that were allocated and not used Data->sp = (AutoSPData *)REALLOC(Data->sp, (Data->num_sp)*sizeof(AutoSPData)); assert(Data->sp); Data->sp_len = Data->num_sp; return 1; }
int main() { realtype abstol=ATOL, reltol=RTOL, t, tout; N_Vector c; WebData wdata; void *cvode_mem; booleantype firstrun; int jpre, gstype, flag; int ns, mxns, iout; c = NULL; wdata = NULL; cvode_mem = NULL; /* Initializations */ c = N_VNew_Serial(NEQ); if(check_flag((void *)c, "N_VNew_Serial", 0)) return(1); wdata = AllocUserData(); if(check_flag((void *)wdata, "AllocUserData", 2)) return(1); InitUserData(wdata); ns = wdata->ns; mxns = wdata->mxns; /* Print problem description */ PrintIntro(); /* Loop over jpre and gstype (four cases) */ for (jpre = PREC_LEFT; jpre <= PREC_RIGHT; jpre++) { for (gstype = MODIFIED_GS; gstype <= CLASSICAL_GS; gstype++) { /* Initialize c and print heading */ CInit(c, wdata); PrintHeader(jpre, gstype); /* Call CVodeInit or CVodeReInit, then CVSpgmr to set up problem */ firstrun = (jpre == PREC_LEFT) && (gstype == MODIFIED_GS); if (firstrun) { cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); wdata->cvode_mem = cvode_mem; flag = CVodeSetUserData(cvode_mem, wdata); if(check_flag(&flag, "CVodeSetUserData", 1)) return(1); flag = CVodeInit(cvode_mem, f, T0, c); if(check_flag(&flag, "CVodeInit", 1)) return(1); flag = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSStolerances", 1)) return(1); flag = CVSpgmr(cvode_mem, jpre, MAXL); if(check_flag(&flag, "CVSpgmr", 1)) return(1); flag = CVSpilsSetGSType(cvode_mem, gstype); if(check_flag(&flag, "CVSpilsSetGSType", 1)) return(1); flag = CVSpilsSetEpsLin(cvode_mem, DELT); if(check_flag(&flag, "CVSpilsSetEpsLin", 1)) return(1); flag = CVSpilsSetPreconditioner(cvode_mem, Precond, PSolve); if(check_flag(&flag, "CVSpilsSetPreconditioner", 1)) return(1); } else { flag = CVodeReInit(cvode_mem, T0, c); if(check_flag(&flag, "CVodeReInit", 1)) return(1); flag = CVSpilsSetPrecType(cvode_mem, jpre); check_flag(&flag, "CVSpilsSetPrecType", 1); flag = CVSpilsSetGSType(cvode_mem, gstype); if(check_flag(&flag, "CVSpilsSetGSType", 1)) return(1); } /* Print initial values */ if (firstrun) PrintAllSpecies(c, ns, mxns, T0); /* Loop over output points, call CVode, print sample solution values. */ tout = T1; for (iout = 1; iout <= NOUT; iout++) { flag = CVode(cvode_mem, tout, c, &t, CV_NORMAL); PrintOutput(cvode_mem, t); if (firstrun && (iout % 3 == 0)) PrintAllSpecies(c, ns, mxns, t); if(check_flag(&flag, "CVode", 1)) break; if (tout > RCONST(0.9)) tout += DTOUT; else tout *= TOUT_MULT; } /* Print final statistics, and loop for next case */ PrintFinalStats(cvode_mem); } } /* Free all memory */ CVodeFree(&cvode_mem); N_VDestroy_Serial(c); FreeUserData(wdata); return(0); }
int main(int argc, char *argv[]) { void *cvode_mem; UserData data; realtype t, tout; N_Vector y; int iout, flag; realtype pbar[NS]; int is; N_Vector *yS; booleantype sensi, err_con; int sensi_meth; cvode_mem = NULL; data = NULL; y = NULL; yS = NULL; /* Process arguments */ ProcessArgs(argc, argv, &sensi, &sensi_meth, &err_con); /* User data structure */ data = (UserData) malloc(sizeof *data); if (check_flag((void *)data, "malloc", 2)) return(1); data->p[0] = RCONST(0.04); data->p[1] = RCONST(1.0e4); data->p[2] = RCONST(3.0e7); /* Initial conditions */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); Ith(y,1) = Y1; Ith(y,2) = Y2; Ith(y,3) = Y3; /* Create CVODES object */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Allocate space for CVODES */ flag = CVodeMalloc(cvode_mem, f, T0, y, CV_WF, 0.0, NULL); if (check_flag(&flag, "CVodeMalloc", 1)) return(1); /* Use private function to compute error weights */ flag = CVodeSetEwtFn(cvode_mem, ewt, NULL); if (check_flag(&flag, "CVodeSetEwtFn", 1)) return(1); /* Attach user data */ flag = CVodeSetFdata(cvode_mem, data); if (check_flag(&flag, "CVodeSetFdata", 1)) return(1); /* Attach linear solver */ flag = CVDense(cvode_mem, NEQ); if (check_flag(&flag, "CVDense", 1)) return(1); flag = CVDenseSetJacFn(cvode_mem, Jac, data); if (check_flag(&flag, "CVDenseSetJacFn", 1)) return(1); printf("\n3-species chemical kinetics problem\n"); /* Sensitivity-related settings */ if (sensi) { pbar[0] = data->p[0]; pbar[1] = data->p[1]; pbar[2] = data->p[2]; yS = N_VNewVectorArray_Serial(NS, NEQ); if (check_flag((void *)yS, "N_VNewVectorArray_Serial", 0)) return(1); for (is=0;is<NS;is++) N_VConst(ZERO, yS[is]); flag = CVodeSensMalloc(cvode_mem, NS, sensi_meth, yS); if(check_flag(&flag, "CVodeSensMalloc", 1)) return(1); flag = CVodeSetSensRhs1Fn(cvode_mem, fS); if (check_flag(&flag, "CVodeSetSensRhs1Fn", 1)) return(1); flag = CVodeSetSensErrCon(cvode_mem, err_con); if (check_flag(&flag, "CVodeSetSensFdata", 1)) return(1); flag = CVodeSetSensFdata(cvode_mem, data); if (check_flag(&flag, "CVodeSetSensFdata", 1)) return(1); flag = CVodeSetSensParams(cvode_mem, NULL, pbar, NULL); if (check_flag(&flag, "CVodeSetSensParams", 1)) return(1); printf("Sensitivity: YES "); if(sensi_meth == CV_SIMULTANEOUS) printf("( SIMULTANEOUS +"); else if(sensi_meth == CV_STAGGERED) printf("( STAGGERED +"); else printf("( STAGGERED1 +"); if(err_con) printf(" FULL ERROR CONTROL )"); else printf(" PARTIAL ERROR CONTROL )"); } else { printf("Sensitivity: NO "); } /* In loop over output points, call CVode, print results, test for error */ printf("\n\n"); printf("==================================================="); printf("============================\n"); printf(" T Q H NST y1"); printf(" y2 y3 \n"); printf("==================================================="); printf("============================\n"); for (iout=1, tout=T1; iout <= NOUT; iout++, tout *= TMULT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); if (check_flag(&flag, "CVode", 1)) break; PrintOutput(cvode_mem, t, y); if (sensi) { flag = CVodeGetSens(cvode_mem, t, yS); if (check_flag(&flag, "CVodeGetSens", 1)) break; PrintOutputS(yS); } printf("-------------------------------------------------"); printf("------------------------------\n"); } /* Print final statistics */ PrintFinalStats(cvode_mem, sensi); /* Free memory */ N_VDestroy_Serial(y); /* Free y vector */ if (sensi) { N_VDestroyVectorArray_Serial(yS, NS); /* Free yS vector */ } free(data); /* Free user data */ CVodeFree(cvode_mem); /* Free CVODES memory */ return(0); }
int main() { realtype abstol, reltol, t, tout; N_Vector u; UserData data; void *cvode_mem; int flag, iout, jpre; long int ml, mu; u = NULL; data = NULL; cvode_mem = NULL; /* Allocate and initialize u, and set problem data and tolerances */ u = N_VNew_Serial(NEQ); if(check_flag((void *)u, "N_VNew_Serial", 0)) return(1); data = (UserData) malloc(sizeof *data); if(check_flag((void *)data, "malloc", 2)) return(1); InitUserData(data); SetInitialProfiles(u, data->dx, data->dy); abstol = ATOL; reltol = RTOL; /* Call CVodeCreate to create the solver memory and specify the * Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Set the pointer to user-defined data */ flag = CVodeSetUserData(cvode_mem, data); if(check_flag(&flag, "CVodeSetUserData", 1)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in u'=f(t,u), the inital time T0, and * the initial dependent variable vector u. */ flag = CVodeInit(cvode_mem, f, T0, u); if(check_flag(&flag, "CVodeInit", 1)) return(1); /* Call CVodeSStolerances to specify the scalar relative tolerance * and scalar absolute tolerances */ flag = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSStolerances", 1)) return(1); /* Call CVSpgmr to specify the linear solver CVSPGMR with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); /* Call CVBandPreInit to initialize band preconditioner */ ml = mu = 2; flag = CVBandPrecInit(cvode_mem, NEQ, mu, ml); if(check_flag(&flag, "CVBandPrecInit", 0)) return(1); PrintIntro(mu, ml); /* Loop over jpre (= PREC_LEFT, PREC_RIGHT), and solve the problem */ for (jpre = PREC_LEFT; jpre <= PREC_RIGHT; jpre++) { /* On second run, re-initialize u, the solver, and CVSPGMR */ if (jpre == PREC_RIGHT) { SetInitialProfiles(u, data->dx, data->dy); flag = CVodeReInit(cvode_mem, T0, u); if(check_flag(&flag, "CVodeReInit", 1)) return(1); flag = CVSpilsSetPrecType(cvode_mem, PREC_RIGHT); check_flag(&flag, "CVSpilsSetPrecType", 1); printf("\n\n-------------------------------------------------------"); printf("------------\n"); } printf("\n\nPreconditioner type is: jpre = %s\n\n", (jpre == PREC_LEFT) ? "PREC_LEFT" : "PREC_RIGHT"); /* In loop over output points, call CVode, print results, test for error */ for (iout = 1, tout = TWOHR; iout <= NOUT; iout++, tout += TWOHR) { flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); PrintOutput(cvode_mem, u, t); if (flag != CV_SUCCESS) { break; } } /* Print final statistics */ PrintFinalStats(cvode_mem); } /* End of jpre loop */ /* Free memory */ N_VDestroy_Serial(u); free(data); CVodeFree(&cvode_mem); return(0); }
int do_integrate(float t_start, float t_stop, int n_points) { realtype reltol, t; N_Vector y, abstol; void *cvode_mem; int flag, flagr, iout; y = abstol = NULL; cvode_mem = NULL; /* Create serial vector of length NEQ for I.C. and abstol */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); abstol = N_VNew_Serial(NEQ); if (check_flag((void *)abstol, "N_VNew_Serial", 0)) return(1); //Setup the initial state values: setup_initial_states(y); setup_tolerances(abstol); /* Set the scalar relative tolerance */ reltol = RTOL; /* Call CVodeCreate to create the solver memory and specify the * Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in y'=f(t,y), the inital time T0, and * the initial dependent variable vector y. */ flag = CVodeInit(cvode_mem, f, t_start, y); if (check_flag(&flag, "CVodeInit", 1)) return(1); /* Call CVodeSVtolerances to specify the scalar relative tolerance * and vector absolute tolerances */ flag = CVodeSVtolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSVtolerances", 1)) return(1); /* Call CVDense to specify the CVDENSE dense linear solver */ flag = CVDense(cvode_mem, NEQ); if (check_flag(&flag, "CVDense", 1)) return(1); /* Set the Jacobian routine to Jac (user-supplied) */ flag = CVDlsSetDenseJacFn(cvode_mem, Jac); if (check_flag(&flag, "CVDlsSetDenseJacFn", 1)) return(1); /* In loop, call CVode, print results, and test for error.*/ printf(" \n3-species kinetics problem\n\n"); iout = 0; int i=0; for(i=1;i< n_points; i++) { float t_next = t_start + (t_stop-t_start)/n_points * i; printf("Advancing to: %f", t_next); flag = CVode(cvode_mem, t_next, y, &t, CV_NORMAL); loop_function(t,y); PrintOutput(t, Ith(y,1), Ith(y,2), Ith(y,3)); //printf("MH: %d %f",iout,t_next); if (check_flag(&flag, "CVode", 1)) break; assert(flag==CV_SUCCESS); } /* Print some final statistics */ PrintFinalStats(cvode_mem); /* Free y and abstol vectors */ N_VDestroy_Serial(y); N_VDestroy_Serial(abstol); /* Free integrator memory */ CVodeFree(&cvode_mem); return(0); }
int main() { void *mem; UserData data; N_Vector uu, up, constraints, res; int ier, iout; realtype rtol, atol, t0, t1, tout, tret; long int netf, ncfn, ncfl; mem = NULL; data = NULL; uu = up = constraints = res = NULL; /* Allocate N-vectors and the user data structure. */ uu = N_VNew_Serial(NEQ); if(check_flag((void *)uu, "N_VNew_Serial", 0)) return(1); up = N_VNew_Serial(NEQ); if(check_flag((void *)up, "N_VNew_Serial", 0)) return(1); res = N_VNew_Serial(NEQ); if(check_flag((void *)res, "N_VNew_Serial", 0)) return(1); constraints = N_VNew_Serial(NEQ); if(check_flag((void *)constraints, "N_VNew_Serial", 0)) return(1); data = (UserData) malloc(sizeof *data); data->pp = NULL; if(check_flag((void *)data, "malloc", 2)) return(1); /* Assign parameters in the user data structure. */ data->mm = MGRID; data->dx = ONE/(MGRID-ONE); data->coeff = ONE/(data->dx * data->dx); data->pp = N_VNew_Serial(NEQ); if(check_flag((void *)data->pp, "N_VNew_Serial", 0)) return(1); /* Initialize uu, up. */ SetInitialProfile(data, uu, up, res); /* Set constraints to all 1's for nonnegative solution values. */ N_VConst(ONE, constraints); /* Assign various parameters. */ t0 = ZERO; t1 = RCONST(0.01); rtol = ZERO; atol = RCONST(1.0e-3); /* Call IDACreate and IDAMalloc to initialize solution */ mem = IDACreate(); if(check_flag((void *)mem, "IDACreate", 0)) return(1); ier = IDASetUserData(mem, data); if(check_flag(&ier, "IDASetUserData", 1)) return(1); ier = IDASetConstraints(mem, constraints); if(check_flag(&ier, "IDASetConstraints", 1)) return(1); N_VDestroy_Serial(constraints); ier = IDAInit(mem, resHeat, t0, uu, up); if(check_flag(&ier, "IDAInit", 1)) return(1); ier = IDASStolerances(mem, rtol, atol); if(check_flag(&ier, "IDASStolerances", 1)) return(1); /* Call IDASpgmr to specify the linear solver. */ ier = IDASpgmr(mem, 0); if(check_flag(&ier, "IDASpgmr", 1)) return(1); ier = IDASpilsSetPreconditioner(mem, PsetupHeat, PsolveHeat); if(check_flag(&ier, "IDASpilsSetPreconditioner", 1)) return(1); /* Print output heading. */ PrintHeader(rtol, atol); /* * ------------------------------------------------------------------------- * CASE I * ------------------------------------------------------------------------- */ /* Print case number, output table heading, and initial line of table. */ printf("\n\nCase 1: gsytpe = MODIFIED_GS\n"); printf("\n Output Summary (umax = max-norm of solution) \n\n"); printf(" time umax k nst nni nje nre nreLS h npe nps\n" ); printf("----------------------------------------------------------------------\n"); /* Loop over output times, call IDASolve, and print results. */ for (tout = t1,iout = 1; iout <= NOUT ; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1)) return(1); PrintOutput(mem, tret, uu); } /* Print remaining counters. */ ier = IDAGetNumErrTestFails(mem, &netf); check_flag(&ier, "IDAGetNumErrTestFails", 1); ier = IDAGetNumNonlinSolvConvFails(mem, &ncfn); check_flag(&ier, "IDAGetNumNonlinSolvConvFails", 1); ier = IDASpilsGetNumConvFails(mem, &ncfl); check_flag(&ier, "IDASpilsGetNumConvFails", 1); printf("\nError test failures = %ld\n", netf); printf("Nonlinear convergence failures = %ld\n", ncfn); printf("Linear convergence failures = %ld\n", ncfl); /* * ------------------------------------------------------------------------- * CASE II * ------------------------------------------------------------------------- */ /* Re-initialize uu, up. */ SetInitialProfile(data, uu, up, res); /* Re-initialize IDA and IDASPGMR */ ier = IDAReInit(mem, t0, uu, up); if(check_flag(&ier, "IDAReInit", 1)) return(1); ier = IDASpilsSetGSType(mem, CLASSICAL_GS); if(check_flag(&ier, "IDASpilsSetGSType",1)) return(1); /* Print case number, output table heading, and initial line of table. */ printf("\n\nCase 2: gstype = CLASSICAL_GS\n"); printf("\n Output Summary (umax = max-norm of solution) \n\n"); printf(" time umax k nst nni nje nre nreLS h npe nps\n" ); printf("----------------------------------------------------------------------\n"); /* Loop over output times, call IDASolve, and print results. */ for (tout = t1,iout = 1; iout <= NOUT ; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1)) return(1); PrintOutput(mem, tret, uu); } /* Print remaining counters. */ ier = IDAGetNumErrTestFails(mem, &netf); check_flag(&ier, "IDAGetNumErrTestFails", 1); ier = IDAGetNumNonlinSolvConvFails(mem, &ncfn); check_flag(&ier, "IDAGetNumNonlinSolvConvFails", 1); ier = IDASpilsGetNumConvFails(mem, &ncfl); check_flag(&ier, "IDASpilsGetNumConvFails", 1); printf("\nError test failures = %ld\n", netf); printf("Nonlinear convergence failures = %ld\n", ncfn); printf("Linear convergence failures = %ld\n", ncfl); /* Free Memory */ IDAFree(&mem); N_VDestroy_Serial(uu); N_VDestroy_Serial(up); N_VDestroy_Serial(res); N_VDestroy_Serial(data->pp); free(data); return(0); }
int loop_function(float t, N_Vector y) { printf("Loop function: %f", t); PrintOutput(t, Ith(y,1), Ith(y,2), Ith(y,3)); }