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() { 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 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(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() { 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 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); /* set the JAcobian-times-vector function */ flag = CVSpilsSetJacTimesVecFn(cvode_mem, jtv); if(check_flag(&flag, "CVSpilsSetJacTimesVecFn", 1)) return(1); /* Set the preconditioner solve and setup functions */ flag = CVSpilsSetPreconditioner(cvode_mem, Precond, PSolve); if(check_flag(&flag, "CVSpilsSetPreconditioner", 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() { 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) { realtype abstol, reltol, t, tout; N_Vector u; UserData data; void *cvode_mem; int linsolver, 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 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); /* START: Loop through SPGMR, SPBCG and SPTFQMR linear solver modules */ for (linsolver = 0; linsolver < 3; ++linsolver) { if (linsolver != 0) { /* Re-initialize user data */ InitUserData(data); SetInitialProfiles(u, data->dx, data->dy); /* Re-initialize CVode for the solution of the same problem, but using a different linear solver module */ flag = CVodeReInit(cvode_mem, T0, u); if (check_flag(&flag, "CVodeReInit", 1)) return(1); } /* Attach a linear solver module */ switch(linsolver) { /* (a) SPGMR */ case(USE_SPGMR): /* Print header */ printf(" -------"); printf(" \n| SPGMR |\n"); printf(" -------\n"); /* 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 = CVSpilsSetGSType(cvode_mem, MODIFIED_GS); if(check_flag(&flag, "CVSpilsSetGSType", 1)) return(1); break; /* (b) SPBCG */ case(USE_SPBCG): /* Print header */ printf(" -------"); printf(" \n| SPBCG |\n"); printf(" -------\n"); /* Call CVSpbcg to specify the linear solver CVSPBCG with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSpbcg(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpbcg", 1)) return(1); break; /* (c) SPTFQMR */ case(USE_SPTFQMR): /* Print header */ printf(" ---------"); printf(" \n| SPTFQMR |\n"); printf(" ---------\n"); /* Call CVSptfqmr to specify the linear solver CVSPTFQMR with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSptfqmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSptfqmr", 1)) return(1); break; } /* Set preconditioner setup and solve routines Precond and PSolve, and the pointer to the user-defined block data */ flag = CVSpilsSetPreconditioner(cvode_mem, Precond, PSolve); if(check_flag(&flag, "CVSpilsSetPreconditioner", 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, linsolver); } /* END: Loop through SPGMR, SPBCG and SPTFQMR linear solver modules */ /* Free memory */ N_VDestroy_Serial(u); FreeUserData(data); CVodeFree(&cvode_mem); return(0); }
int main(int argc, char *argv[]) { int globalstrategy; long int local_N; realtype fnormtol, scsteptol; N_Vector cc, sc, constraints; UserData data; int flag, maxl, maxlrst; int my_pe, npes, npelast = NPEX*NPEY-1; void *kmem; MPI_Comm comm; cc = sc = constraints = NULL; data = NULL; kmem = 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) fprintf(stderr, "\nMPI_ERROR(0); npes = %d is not equal to NPEX*NPEY = %d\n", npes,NPEX*NPEY); MPI_Finalize(); return(1); } /* Allocate memory, and set problem data, initial values, tolerances */ /* Set local vector length */ local_N = NUM_SPECIES*MXSUB*MYSUB; /* Allocate and initialize user data block */ data = AllocUserData(); if (check_flag((void *)data, "AllocUserData", 0, my_pe)) MPI_Abort(comm, 1); InitUserData(my_pe, comm, data); /* Set global strategy flag */ globalstrategy = KIN_NONE; /* Allocate and initialize vectors */ cc = N_VNew_Parallel(comm, local_N, NEQ); if (check_flag((void *)cc, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); sc = N_VNew_Parallel(comm, local_N, NEQ); if (check_flag((void *)sc, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); data->rates = N_VNew_Parallel(comm, local_N, NEQ); if (check_flag((void *)data->rates, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); constraints = N_VNew_Parallel(comm, local_N, 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/KINMalloc to initialize KINSOL: nvSpec is the nvSpec pointer used in the parallel version 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 = KINMalloc(kmem, funcprpr, cc); if (check_flag(&flag, "KINMalloc", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetNumMaxIters(kmem, 250); if (check_flag(&flag, "KINSetNumMaxIters", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetFdata(kmem, data); if (check_flag(&flag, "KINSetFdata", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetConstraints(kmem, constraints); if (check_flag(&flag, "KINSetConstraints", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTop", 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); /* Call KINSpgmr to specify the linear solver KINSPGMR with preconditioner routines Precondbd and PSolvebd, and the pointer to the user data block. */ maxl = 20; maxlrst = 2; flag = KINSpgmr(kmem, maxl); if (check_flag(&flag, "KINSpgmr", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSpilsSetMaxRestarts(kmem, maxlrst); if (check_flag(&flag, "KINSpilsSetMaxRestarts", 1, my_pe)) MPI_Abort(comm, 1); flag = KINSpilsSetPreconditioner(kmem, Precondbd, PSolvebd, data); if (check_flag(&flag, "KINSpilsSetPreconditioner", 1, my_pe)) MPI_Abort(comm, 1); /* Print out the problem size, solution parameters, initial guess. */ if (my_pe == 0) 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, 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[]) { MPI_Comm comm; void *mem; UserData webdata; long int SystemSize, local_N; realtype rtol, atol, t0, tout, tret; N_Vector cc, cp, res, id; int thispe, npes, maxl, iout, flag; 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 = AllocUserData(comm, local_N, SystemSize); if (check_flag((void *)webdata, "AllocUserData", 0, thispe)) MPI_Abort(comm, 1); 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(res); /* Set remaining inputs to IDAMalloc. */ t0 = ZERO; rtol = RTOL; atol = ATOL; /* Call IDACreate and IDAMalloc to initialize IDA. A pointer to IDA problem memory is returned and stored in idamem. */ mem = IDACreate(); if (check_flag((void *)mem, "IDACreate", 0, thispe)) MPI_Abort(comm, 1); flag = IDASetUserData(mem, webdata); if (check_flag(&flag, "IDASetUserData", 1, thispe)) MPI_Abort(comm, 1); flag = IDASetId(mem, id); if (check_flag(&flag, "IDASetId", 1, thispe)) MPI_Abort(comm, 1); flag = IDAInit(mem, resweb, t0, cc, cp); if (check_flag(&flag, "IDAinit", 1, thispe)) MPI_Abort(comm, 1); flag = IDASStolerances(mem, rtol, atol); if (check_flag(&flag, "IDASStolerances", 1, thispe)) MPI_Abort(comm, 1); webdata->ida_mem = mem; /* Call IDASpgmr to specify the IDA linear solver IDASPGMR and specify the preconditioner routines supplied (Precondbd and PSolvebd). maxl (max. Krylov subspace dim.) is set to 16. */ maxl = 16; flag = IDASpgmr(mem, maxl); if (check_flag(&flag, "IDASpgmr", 1, thispe)) MPI_Abort(comm, 1); flag = IDASpilsSetPreconditioner(mem, Precondbd, PSolvebd); if (check_flag(&flag, "IDASpilsSetPreconditioner", 1, thispe)) MPI_Abort(comm, 1); /* Call IDACalcIC (with default options) to correct the initial values. */ tout = RCONST(0.001); flag = IDACalcIC(mem, IDA_YA_YDP_INIT, tout); if (check_flag(&flag, "IDACalcIC", 1, thispe)) MPI_Abort(comm, 1); /* On PE 0, print heading, basic parameters, initial values. */ if (thispe == 0) PrintHeader(SystemSize, maxl, rtol, atol); PrintOutput(mem, cc, t0, webdata, comm); /* Loop over iout, call IDASolve (normal mode), print selected output. */ for (iout = 1; iout <= NOUT; iout++) { flag = IDASolve(mem, tout, &tret, cc, cp, IDA_NORMAL); if (check_flag(&flag, "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); FreeUserData(webdata); MPI_Finalize(); return(0); }
int main(int argc, char *argv[]) { realtype abstol=ATOL, reltol=RTOL, t; N_Vector c; WebData wdata; void *cvode_mem; SUNLinearSolver LS, LSB; int retval, ncheck; int indexB; realtype reltolB=RTOL, abstolB=ATOL; N_Vector cB; c = NULL; cB = NULL; wdata = NULL; cvode_mem = NULL; LS = LSB = NULL; /* Allocate and initialize user data */ wdata = AllocUserData(); if(check_retval((void *)wdata, "AllocUserData", 2)) return(1); InitUserData(wdata); /* Set-up forward problem */ /* Initializations */ c = N_VNew_Serial(NEQ+1); if(check_retval((void *)c, "N_VNew_Serial", 0)) return(1); CInit(c, wdata); /* Call CVodeCreate/CVodeInit for forward run */ printf("\nCreate and allocate CVODES memory for forward run\n"); cvode_mem = CVodeCreate(CV_BDF); if(check_retval((void *)cvode_mem, "CVodeCreate", 0)) return(1); wdata->cvode_mem = cvode_mem; /* Used in Precond */ retval = CVodeSetUserData(cvode_mem, wdata); if(check_retval(&retval, "CVodeSetUserData", 1)) return(1); retval = CVodeInit(cvode_mem, f, T0, c); if(check_retval(&retval, "CVodeInit", 1)) return(1); retval = CVodeSStolerances(cvode_mem, reltol, abstol); if(check_retval(&retval, "CVodeSStolerances", 1)) return(1); /* Create SUNLinSol_SPGMR linear solver for forward run */ LS = SUNLinSol_SPGMR(c, PREC_LEFT, 0); if(check_retval((void *)LS, "SUNLinSol_SPGMR", 0)) return(1); /* Attach the linear sovler */ retval = CVodeSetLinearSolver(cvode_mem, LS, NULL); if (check_retval(&retval, "CVodeSetLinearSolver", 1)) return 1; /* Set the preconditioner solve and setup functions */ retval = CVodeSetPreconditioner(cvode_mem, Precond, PSolve); if(check_retval(&retval, "CVodeSetPreconditioner", 1)) return(1); /* Set-up adjoint calculations */ printf("\nAllocate global memory\n"); retval = CVodeAdjInit(cvode_mem, NSTEPS, CV_HERMITE); if(check_retval(&retval, "CVadjInit", 1)) return(1); /* Perform forward run */ printf("\nForward integration\n"); retval = CVodeF(cvode_mem, TOUT, c, &t, CV_NORMAL, &ncheck); if(check_retval(&retval, "CVodeF", 1)) return(1); printf("\nncheck = %d\n", ncheck); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("\n G = int_t int_x int_y c%d(t,x,y) dx dy dt = %Lf \n\n", ISPEC, N_VGetArrayPointer(c)[NEQ]); #else printf("\n G = int_t int_x int_y c%d(t,x,y) dx dy dt = %f \n\n", ISPEC, N_VGetArrayPointer(c)[NEQ]); #endif /* Set-up backward problem */ /* Allocate cB */ cB = N_VNew_Serial(NEQ); if(check_retval((void *)cB, "N_VNew_Serial", 0)) return(1); /* Initialize cB = 0 */ N_VConst(ZERO, cB); /* 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, wdata); if(check_retval(&retval, "CVodeSetUserDataB", 1)) return(1); retval = CVodeSetMaxNumStepsB(cvode_mem, indexB, 1000); if(check_retval(&retval, "CVodeSetMaxNumStepsB", 1)) return(1); retval = CVodeInitB(cvode_mem, indexB, fB, TOUT, cB); if(check_retval(&retval, "CVodeInitB", 1)) return(1); retval = CVodeSStolerancesB(cvode_mem, indexB, reltolB, abstolB); if(check_retval(&retval, "CVodeSStolerancesB", 1)) return(1); wdata->indexB = indexB; /* Create SUNLinSol_SPGMR linear solver for backward run */ LSB = SUNLinSol_SPGMR(cB, PREC_LEFT, 0); if(check_retval((void *)LSB, "SUNLinSol_SPGMR", 0)) return(1); /* Attach the linear sovler */ retval = CVodeSetLinearSolverB(cvode_mem, indexB, LSB, NULL); if (check_retval(&retval, "CVodeSetLinearSolverB", 1)) return 1; /* Set the preconditioner solve and setup functions */ retval = CVodeSetPreconditionerB(cvode_mem, indexB, PrecondB, PSolveB); if(check_retval(&retval, "CVodeSetPreconditionerB", 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, cB); if(check_retval(&retval, "CVodeGetB", 1)) return(1); PrintOutput(cB, NS, MXNS, wdata); /* Free all memory */ CVodeFree(&cvode_mem); N_VDestroy(c); N_VDestroy(cB); SUNLinSolFree(LS); SUNLinSolFree(LSB); FreeUserData(wdata); return(0); }
int main(int argc, char *argv[]) { realtype abstol=ATOL, reltol=RTOL, t; N_Vector c; WebData wdata; void *cvode_mem; int flag; void *cvadj_mem; int ncheck; realtype reltolB=RTOL, abstolB=ATOL; N_Vector cB; c = NULL; cB = NULL; wdata = NULL; cvode_mem = NULL; cvadj_mem = NULL; /* Allocate and initialize user data */ wdata = AllocUserData(); if(check_flag((void *)wdata, "AllocUserData", 2)) return(1); InitUserData(wdata); /* Set-up forward problem */ /* Initializations */ c = N_VNew_Serial(NEQ+1); if(check_flag((void *)c, "N_VNew_Serial", 0)) return(1); CInit(c, wdata); /* Call CVodeCreate/CVodeMalloc for forward run */ printf("\nCreate and allocate CVODE memory for forward run\n"); cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); wdata->cvode_memF = cvode_mem; /* Used in Precond */ flag = CVodeSetFdata(cvode_mem, wdata); if(check_flag(&flag, "CVodeSetFdata", 1)) return(1); flag = CVodeMalloc(cvode_mem, f, T0, c, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); /* Call CVSpgmr for forward run */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); flag = CVSpgmrSetPreconditioner(cvode_mem, Precond, PSolve, wdata); if(check_flag(&flag, "CVSpgmrSetPreconditioner", 1)) return(1); /* Set-up adjoint calculations */ printf("\nAllocate global memory\n"); cvadj_mem = CVadjMalloc(cvode_mem, NSTEPS); if(check_flag((void *)cvadj_mem, "CVadjMalloc", 0)) return(1); wdata->cvadj_mem = cvadj_mem; /* Perform forward run */ printf("\nForward integration ... "); flag = CVodeF(cvadj_mem, TOUT, c, &t, CV_NORMAL, &ncheck); if(check_flag(&flag, "CVodeF", 1)) return(1); printf("done (ncheck = %d)\n",ncheck); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("\n G = int_t int_x int_y c%d(t,x,y) dx dy dt = %Lf \n\n", ISPEC, NV_DATA_S(c)[NEQ]); #else printf("\n G = int_t int_x int_y c%d(t,x,y) dx dy dt = %f \n\n", ISPEC, NV_DATA_S(c)[NEQ]); #endif /* Set-up backward problem */ /* Allocate cB */ cB = N_VNew_Serial(NEQ); if(check_flag((void *)cB, "N_VNew_Serial", 0)) return(1); /* Initialize cB = 0 */ N_VConst(ZERO, cB); /* Create and allocate CVODES memory for backward run */ printf("\nCreate and allocate CVODES memory for backward run\n"); flag = CVodeCreateB(cvadj_mem, CV_BDF, CV_NEWTON); if(check_flag(&flag, "CVodeCreateB", 1)) return(1); flag = CVodeSetFdataB(cvadj_mem, wdata); if(check_flag(&flag, "CVodeSetFdataB", 1)) return(1); flag = CVodeMallocB(cvadj_mem, fB, TOUT, cB, CV_SS, reltolB, &abstolB); if(check_flag(&flag, "CVodeMallocB", 1)) return(1); /* Call CVSpgmr */ flag = CVSpgmrB(cvadj_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmrB", 1)) return(1); flag = CVSpgmrSetPreconditionerB(cvadj_mem, PrecondB, PSolveB, wdata); if(check_flag(&flag, "CVSpgmrSetPreconditionerB", 1)) return(1); /* Perform backward integration */ printf("\nBackward integration\n"); flag = CVodeB(cvadj_mem, T0, cB, &t, CV_NORMAL); if(check_flag(&flag, "CVodeB", 1)) return(1); PrintOutput(cB, NS, MXNS, wdata); /* Free all memory */ CVodeFree(cvode_mem); CVadjFree(cvadj_mem); N_VDestroy_Serial(c); N_VDestroy_Serial(cB); FreeUserData(wdata); return(0); }