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[]) { ProblemData d; MPI_Comm comm; int npes, npes_needed; int myId; long int neq, l_neq; void *cvode_mem; N_Vector y, q; realtype abstol, reltol, abstolQ, reltolQ; long int mudq, mldq, mukeep, mlkeep; int indexB; N_Vector yB, qB; realtype abstolB, reltolB, abstolQB, reltolQB; long int mudqB, mldqB, mukeepB, mlkeepB; realtype tret, *qdata, G; int ncheckpnt, flag; booleantype output; /* Initialize MPI and set Ids */ MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_rank(comm, &myId); /* Check number of processes */ npes_needed = NPX * NPY; #ifdef USE3D npes_needed *= NPZ; #endif MPI_Comm_size(comm, &npes); if (npes_needed != npes) { if (myId == 0) fprintf(stderr,"I need %d processes but I only got %d\n", npes_needed, npes); MPI_Abort(comm, EXIT_FAILURE); } /* Test if matlab output is requested */ if (argc > 1) output = TRUE; else output = FALSE; /* Allocate and set problem data structure */ d = (ProblemData) malloc(sizeof *d); SetData(d, comm, npes, myId, &neq, &l_neq); if (myId == 0) PrintHeader(); /*-------------------------- Forward integration phase --------------------------*/ /* Allocate space for y and set it with the I.C. */ y = N_VNew_Parallel(comm, l_neq, neq); N_VConst(ZERO, y); /* Allocate and initialize qB (local contribution to cost) */ q = N_VNew_Parallel(comm, 1, npes); N_VConst(ZERO, q); /* Create CVODES object, attach user data, and allocate space */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); flag = CVodeSetUserData(cvode_mem, d); flag = CVodeInit(cvode_mem, f, ti, y); abstol = ATOL; reltol = RTOL; flag = CVodeSStolerances(cvode_mem, reltol, abstol); /* attach linear solver */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); /* Attach preconditioner and linear solver modules */ mudq = mldq = d->l_m[0]+1; mukeep = mlkeep = 2; flag = CVBBDPrecInit(cvode_mem, l_neq, mudq, mldq, mukeep, mlkeep, ZERO, f_local, NULL); /* Initialize quadrature calculations */ abstolQ = ATOL_Q; reltolQ = RTOL_Q; flag = CVodeQuadInit(cvode_mem, fQ, q); flag = CVodeQuadSStolerances(cvode_mem, reltolQ, abstolQ); flag = CVodeSetQuadErrCon(cvode_mem, TRUE); /* Allocate space for the adjoint calculation */ flag = CVodeAdjInit(cvode_mem, STEPS, CV_HERMITE); /* Integrate forward in time while storing check points */ if (myId == 0) printf("Begin forward integration... "); flag = CVodeF(cvode_mem, tf, y, &tret, CV_NORMAL, &ncheckpnt); if (myId == 0) printf("done. "); /* Extract quadratures */ flag = CVodeGetQuad(cvode_mem, &tret, q); qdata = NV_DATA_P(q); MPI_Allreduce(&qdata[0], &G, 1, PVEC_REAL_MPI_TYPE, MPI_SUM, comm); #if defined(SUNDIALS_EXTENDED_PRECISION) if (myId == 0) printf(" G = %Le\n",G); #elif defined(SUNDIALS_DOUBLE_PRECISION) if (myId == 0) printf(" G = %e\n",G); #else if (myId == 0) printf(" G = %e\n",G); #endif /* Print statistics for forward run */ if (myId == 0) PrintFinalStats(cvode_mem); /*-------------------------- Backward integration phase --------------------------*/ /* Allocate and initialize yB */ yB = N_VNew_Parallel(comm, l_neq, neq); N_VConst(ZERO, yB); /* Allocate and initialize qB (gradient) */ qB = N_VNew_Parallel(comm, l_neq, neq); N_VConst(ZERO, qB); /* Create and allocate backward CVODE memory */ flag = CVodeCreateB(cvode_mem, CV_BDF, CV_NEWTON, &indexB); flag = CVodeSetUserDataB(cvode_mem, indexB, d); flag = CVodeInitB(cvode_mem, indexB, fB, tf, yB); abstolB = ATOL_B; reltolB = RTOL_B; flag = CVodeSStolerancesB(cvode_mem, indexB, reltolB, abstolB); /* Attach preconditioner and linear solver modules */ flag = CVSpgmrB(cvode_mem, indexB, PREC_LEFT, 0); mudqB = mldqB = d->l_m[0]+1; mukeepB = mlkeepB = 2; flag = CVBBDPrecInitB(cvode_mem, indexB, l_neq, mudqB, mldqB, mukeepB, mlkeepB, ZERO, fB_local, NULL); /* Initialize quadrature calculations */ abstolQB = ATOL_QB; reltolQB = RTOL_QB; flag = CVodeQuadInitB(cvode_mem, indexB, fQB, qB); flag = CVodeQuadSStolerancesB(cvode_mem, indexB, reltolQB, abstolQB); flag = CVodeSetQuadErrConB(cvode_mem, indexB, TRUE); /* Integrate backwards */ if (myId == 0) printf("Begin backward integration... "); flag = CVodeB(cvode_mem, ti, CV_NORMAL); if (myId == 0) printf("done.\n"); /* Extract solution */ flag = CVodeGetB(cvode_mem, indexB, &tret, yB); /* Extract quadratures */ flag = CVodeGetQuadB(cvode_mem, indexB, &tret, qB); /* Print statistics for backward run */ if (myId == 0) { PrintFinalStats(CVodeGetAdjCVodeBmem(cvode_mem, indexB)); } /* Process 0 collects the gradient components and prints them */ if (output) { OutputGradient(myId, qB, d); if (myId == 0) printf("Wrote matlab file 'grad.m'.\n"); } /* Free memory */ N_VDestroy_Parallel(y); N_VDestroy_Parallel(q); N_VDestroy_Parallel(qB); N_VDestroy_Parallel(yB); CVodeFree(&cvode_mem); 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(int argc, char *argv[]) { UserData data; void *cvode_mem; realtype reltolQ, abstolQ; N_Vector y, q; int steps; int indexB; realtype reltolB, abstolB, abstolQB; N_Vector yB, qB; realtype time; int flag, ncheck; long int nst, nstB; CVadjCheckPointRec *ckpnt; data = NULL; cvode_mem = NULL; ckpnt = NULL; y = yB = qB = 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_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); /* Initialize y */ y = N_VNew_Serial(NEQ); if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1); Ith(y,1) = RCONST(1.0); Ith(y,2) = ZERO; Ith(y,3) = ZERO; /* Initialize q */ q = N_VNew_Serial(1); if (check_flag((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"); cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if (check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); flag = CVodeInit(cvode_mem, f, T0, y); if (check_flag(&flag, "CVodeInit", 1)) return(1); flag = CVodeWFtolerances(cvode_mem, ewt); if (check_flag(&flag, "CVodeWFtolerances", 1)) return(1); flag = CVodeSetUserData(cvode_mem, data); if (check_flag(&flag, "CVodeSetUserData", 1)) return(1); flag = CVDense(cvode_mem, NEQ); if (check_flag(&flag, "CVDense", 1)) return(1); flag = CVDlsSetDenseJacFn(cvode_mem, Jac); if (check_flag(&flag, "CVDlsSetDenseJacFn", 1)) return(1); flag = CVodeQuadInit(cvode_mem, fQ, q); if (check_flag(&flag, "CVodeQuadInit", 1)) return(1); flag = CVodeQuadSStolerances(cvode_mem, reltolQ, abstolQ); if (check_flag(&flag, "CVodeQuadSStolerances", 1)) return(1); flag = CVodeSetQuadErrCon(cvode_mem, TRUE); if (check_flag(&flag, "CVodeSetQuadErrCon", 1)) return(1); /* Allocate global memory */ steps = STEPS; flag = CVodeAdjInit(cvode_mem, steps, CV_HERMITE); /* flag = CVodeAdjInit(cvode_mem, steps, CV_POLYNOMIAL); */ if (check_flag(&flag, "CVodeAdjInit", 1)) return(1); /* Perform forward run */ printf("Forward integration ... "); flag = CVodeF(cvode_mem, TOUT, y, &time, CV_NORMAL, &ncheck); if (check_flag(&flag, "CVodeF", 1)) return(1); flag = CVodeGetNumSteps(cvode_mem, &nst); if (check_flag(&flag, "CVodeGetNumSteps", 1)) return(1); printf("done ( nst = %ld )\n",nst); flag = CVodeGetQuad(cvode_mem, &time, q); if (check_flag(&flag, "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.4le \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_flag((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_flag((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; /* Create and allocate CVODES memory for backward run */ printf("Create and allocate CVODES memory for backward run\n"); flag = CVodeCreateB(cvode_mem, CV_BDF, CV_NEWTON, &indexB); if (check_flag(&flag, "CVodeCreateB", 1)) return(1); flag = CVodeInitB(cvode_mem, indexB, fB, TB1, yB); if (check_flag(&flag, "CVodeInitB", 1)) return(1); flag = CVodeSStolerancesB(cvode_mem, indexB, reltolB, abstolB); if (check_flag(&flag, "CVodeSStolerancesB", 1)) return(1); flag = CVodeSetUserDataB(cvode_mem, indexB, data); if (check_flag(&flag, "CVodeSetUserDataB", 1)) return(1); flag = CVDenseB(cvode_mem, indexB, NEQ); if (check_flag(&flag, "CVDenseB", 1)) return(1); flag = CVDlsSetDenseJacFnB(cvode_mem, indexB, JacB); if (check_flag(&flag, "CVDlsSetDenseJacFnB", 1)) return(1); flag = CVodeQuadInitB(cvode_mem, indexB, fQB, qB); if (check_flag(&flag, "CVodeQuadInitB", 1)) return(1); flag = CVodeQuadSStolerancesB(cvode_mem, indexB, reltolB, abstolQB); if (check_flag(&flag, "CVodeQuadSStolerancesB", 1)) return(1); flag = CVodeSetQuadErrConB(cvode_mem, indexB, TRUE); if (check_flag(&flag, "CVodeSetQuadErrConB", 1)) return(1); /* Backward Integration */ printf("Backward integration ... "); flag = CVodeB(cvode_mem, T0, CV_NORMAL); if (check_flag(&flag, "CVodeB", 1)) return(1); CVodeGetNumSteps(CVodeGetAdjCVodeBmem(cvode_mem, indexB), &nstB); printf("done ( nst = %ld )\n", nstB); flag = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_flag(&flag, "CVodeGetB", 1)) return(1); flag = CVodeGetQuadB(cvode_mem, indexB, &time, qB); if (check_flag(&flag, "CVodeGetQuadB", 1)) return(1); PrintOutput(TB1, 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"); flag = CVodeReInitB(cvode_mem, indexB, TB2, yB); if (check_flag(&flag, "CVodeReInitB", 1)) return(1); flag = CVodeQuadReInitB(cvode_mem, indexB, qB); if (check_flag(&flag, "CVodeQuadReInitB", 1)) return(1); printf("Backward integration ... "); flag = CVodeB(cvode_mem, T0, CV_NORMAL); if (check_flag(&flag, "CVodeB", 1)) return(1); CVodeGetNumSteps(CVodeGetAdjCVodeBmem(cvode_mem, indexB), &nstB); printf("done ( nst = %ld )\n", nstB); flag = CVodeGetB(cvode_mem, indexB, &time, yB); if (check_flag(&flag, "CVodeGetB", 1)) return(1); flag = CVodeGetQuadB(cvode_mem, indexB, &time, qB); if (check_flag(&flag, "CVodeGetQuadB", 1)) return(1); PrintOutput(TB2, yB, qB); /* Free memory */ printf("Free memory\n\n"); CVodeFree(&cvode_mem); N_VDestroy_Serial(y); N_VDestroy_Serial(q); N_VDestroy_Serial(yB); N_VDestroy_Serial(qB); if (ckpnt != NULL) free(ckpnt); free(data); 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[]) { UserData data; void *cvode_mem; N_Vector u; realtype reltol, abstol; int indexB; N_Vector uB; realtype dx, t, g_val; int retval, my_pe, nprocs, npes, ncheck; sunindextype local_N=0, nperpe, nrem, my_base=-1; SUNNonlinearSolver NLS, NLSB; MPI_Comm comm; data = NULL; cvode_mem = NULL; u = uB = NULL; /*------------------------------------------------------ Initialize MPI and get total number of pe's, and my_pe ------------------------------------------------------*/ MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &nprocs); MPI_Comm_rank(comm, &my_pe); npes = nprocs - 1; /* pe's dedicated to PDE integration */ if ( npes <= 0 ) { if (my_pe == npes) fprintf(stderr, "\nMPI_ERROR(%d): number of processes must be >= 2\n\n", my_pe); MPI_Finalize(); return(1); } /*----------------------- Set local vector length -----------------------*/ nperpe = NEQ/npes; nrem = NEQ - npes*nperpe; if (my_pe < npes) { /* PDE vars. distributed to this proccess */ local_N = (my_pe < nrem) ? nperpe+1 : nperpe; my_base = (my_pe < nrem) ? my_pe*local_N : my_pe*nperpe + nrem; } else { /* Make last process inactive for forward phase */ local_N = 0; } /*------------------------------------- Allocate and load user data structure -------------------------------------*/ data = (UserData) malloc(sizeof *data); if (check_retval((void *)data , "malloc", 2, my_pe)) MPI_Abort(comm, 1); data->p[0] = ONE; data->p[1] = RCONST(0.5); dx = data->dx = XMAX/((realtype)(MX+1)); data->hdcoef = data->p[0]/(dx*dx); data->hacoef = data->p[1]/(TWO*dx); data->comm = comm; data->npes = npes; data->my_pe = my_pe; data->nperpe = nperpe; data->nrem = nrem; data->local_N = local_N; /*------------------------- Forward integration phase -------------------------*/ /* Set relative and absolute tolerances for forward phase */ reltol = ZERO; abstol = ATOL; /* Allocate and initialize forward variables */ u = N_VNew_Parallel(comm, local_N, NEQ); if (check_retval((void *)u, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); SetIC(u, dx, local_N, my_base); /* Allocate CVODES memory for forward integration */ cvode_mem = CVodeCreate(CV_ADAMS); if (check_retval((void *)cvode_mem, "CVodeCreate", 0, my_pe)) MPI_Abort(comm, 1); retval = CVodeSetUserData(cvode_mem, data); if (check_retval(&retval, "CVodeSetUserData", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeInit(cvode_mem, f, T0, u); if (check_retval(&retval, "CVodeInit", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_retval(&retval, "CVodeSStolerances", 1, my_pe)) MPI_Abort(comm, 1); /* create fixed point nonlinear solver object */ NLS = SUNNonlinSol_FixedPoint(u, 0); if(check_retval((void *)NLS, "SUNNonlinSol_FixedPoint", 0, my_pe)) MPI_Abort(comm, 1); /* attach nonlinear solver object to CVode */ retval = CVodeSetNonlinearSolver(cvode_mem, NLS); if(check_retval(&retval, "CVodeSetNonlinearSolver", 1, my_pe)) MPI_Abort(comm, 1); /* Allocate combined forward/backward memory */ retval = CVodeAdjInit(cvode_mem, STEPS, CV_HERMITE); if (check_retval(&retval, "CVadjInit", 1, my_pe)) MPI_Abort(comm, 1); /* Integrate to TOUT and collect check point information */ retval = CVodeF(cvode_mem, TOUT, u, &t, CV_NORMAL, &ncheck); if (check_retval(&retval, "CVodeF", 1, my_pe)) MPI_Abort(comm, 1); /*--------------------------- Compute and value of g(t_f) ---------------------------*/ g_val = Compute_g(u, data); /*-------------------------- Backward integration phase --------------------------*/ if (my_pe == npes) { /* Activate last process for integration of the quadrature equations */ local_N = NP; } else { /* Allocate work space */ data->z1 = (realtype *)malloc(local_N*sizeof(realtype)); if (check_retval((void *)data->z1, "malloc", 2, my_pe)) MPI_Abort(comm, 1); data->z2 = (realtype *)malloc(local_N*sizeof(realtype)); if (check_retval((void *)data->z2, "malloc", 2, my_pe)) MPI_Abort(comm, 1); } /* Allocate and initialize backward variables */ uB = N_VNew_Parallel(comm, local_N, NEQ+NP); if (check_retval((void *)uB, "N_VNew_Parallel", 0, my_pe)) MPI_Abort(comm, 1); SetICback(uB, my_base); /* Allocate CVODES memory for the backward integration */ retval = CVodeCreateB(cvode_mem, CV_ADAMS, &indexB); if (check_retval(&retval, "CVodeCreateB", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeSetUserDataB(cvode_mem, indexB, data); if (check_retval(&retval, "CVodeSetUserDataB", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeInitB(cvode_mem, indexB, fB, TOUT, uB); if (check_retval(&retval, "CVodeInitB", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeSStolerancesB(cvode_mem, indexB, reltol, abstol); if (check_retval(&retval, "CVodeSStolerancesB", 1, my_pe)) MPI_Abort(comm, 1); /* create fixed point nonlinear solver object */ NLSB = SUNNonlinSol_FixedPoint(uB, 0); if(check_retval((void *)NLSB, "SUNNonlinSol_FixedPoint", 0, my_pe)) MPI_Abort(comm, 1); /* attach nonlinear solver object to CVode */ retval = CVodeSetNonlinearSolverB(cvode_mem, indexB, NLSB); if(check_retval(&retval, "CVodeSetNonlinearSolver", 1, my_pe)) MPI_Abort(comm, 1); /* Integrate to T0 */ retval = CVodeB(cvode_mem, T0, CV_NORMAL); if (check_retval(&retval, "CVodeB", 1, my_pe)) MPI_Abort(comm, 1); retval = CVodeGetB(cvode_mem, indexB, &t, uB); if (check_retval(&retval, "CVodeGetB", 1, my_pe)) MPI_Abort(comm, 1); /* Print results (adjoint states and quadrature variables) */ PrintOutput(g_val, uB, data); /* Free memory */ N_VDestroy_Parallel(u); N_VDestroy_Parallel(uB); CVodeFree(&cvode_mem); SUNNonlinSolFree(NLS); SUNNonlinSolFree(NLSB); if (my_pe != npes) { free(data->z1); free(data->z2); } free(data); MPI_Finalize(); return(0); }