/* nag_pde_parab_1d_fd_ode_remesh (d03ppc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void NAG_CALL pdedef(Integer, double, double, const double[], const double[], Integer, const double[], const double[], double[], double[], double[], Integer *, Nag_Comm *); static void NAG_CALL bndary(Integer, double, const double[], const double[], Integer, const double[], const double[], Integer, double[], double[], Integer *, Nag_Comm *); static void NAG_CALL uvinit(Integer, Integer, Integer, const double[], const double[], double[], Integer, double[], Nag_Comm *); static void NAG_CALL monitf(double, Integer, Integer, const double[], const double[], const double[], double[], Nag_Comm *); #ifdef __cplusplus } #endif static void NAG_CALL exact(double, double *, Integer, double *, Nag_Comm *); #define P(I, J) p[npde*((J) -1)+(I) -1] #define R(I, J) r[npde*((J) -1)+(I) -1] #define U(I, J) u[npde*((J) -1)+(I) -1] #define UOUT(I, J, K) uout[npde*(intpts*((K) -1)+(J) -1)+(I) -1] int main(int argc, char *argv[]) { FILE *fpout; const Integer npde = 1, npts = 61, ncode = 0, m = 0, nxi = 0, nxfix = 0; const Integer itype = 1, neqn = npde*npts+ncode, intpts = 5; const Integer lisave = 25+nxfix; const Integer nwkres = npde*(npts+3*npde+21)+7*npts+nxfix+3; const Integer lenode = 11*neqn+50, lrsave = neqn*neqn+neqn+nwkres+lenode; double con, dxmesh, e, tout, trmesh, ts, xratio; Integer exit_status, i, ind, ipminf, it, itask, itol, itrace, nrmesh; Nag_Boolean remesh, theta; double *algopt = 0, *atol = 0, *rsave = 0, *rtol = 0, *u = 0, *ue = 0; double *uout = 0, *x = 0, *xfix = 0, *xi = 0, *xout = 0; Integer *isave = 0; NagError fail; Nag_Comm comm; Nag_D03_Save saved; INIT_FAIL(fail); /* Check for command-line IO options */ fpout = nag_example_file_io(argc, argv, "-results", NULL); exit_status = 0; /* Allocate memory */ if (!(algopt = NAG_ALLOC(30, double)) || !(atol = NAG_ALLOC(1, double)) || !(rsave = NAG_ALLOC(lrsave, double)) || !(rtol = NAG_ALLOC(1, double)) || !(u = NAG_ALLOC(neqn, double)) || !(ue = NAG_ALLOC(intpts, double)) || !(uout = NAG_ALLOC(npde*intpts*itype, double)) || !(x = NAG_ALLOC(npts, double)) || !(xfix = NAG_ALLOC(1, double)) || !(xi = NAG_ALLOC(1, double)) || !(xout = NAG_ALLOC(intpts, double)) || !(isave = NAG_ALLOC(lisave, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = 1; goto END; } fprintf(fpout, "nag_pde_parab_1d_fd_ode_remesh (d03ppc) Example Program" " Results\n\n"); e = 0.005; comm.p = (Pointer)&e; itrace = 0; itol = 1; atol[0] = 5e-5; rtol[0] = atol[0]; fprintf(fpout, " Accuracy requirement =%12.3e", atol[0]); fprintf(fpout, " Number of points = %3ld\n\n", npts); /* Initialise mesh */ for (i = 0; i < npts; ++i) x[i] = i/(npts-1.0); /* Set remesh parameters */ remesh = Nag_TRUE; nrmesh = 3; dxmesh = 0.5; trmesh = 0.0; con = 2.0/(npts-1.0); xratio = 1.5; ipminf = 0; fprintf(fpout, " Remeshing every %3ld time steps\n\n", nrmesh); fprintf(fpout, " e =%8.3f\n\n\n", e); xi[0] = 0.0; ind = 0; itask = 1; /* Set theta to TRUE if the Theta integrator is required */ theta = Nag_FALSE; for (i = 0; i < 30; ++i) algopt[i] = 0.0; if (theta) { algopt[0] = 2.0; } else { algopt[0] = 0.0; } /* Loop over output value of t */ ts = 0.0; tout = 0.0; for (it = 0; it < 5; ++it) { tout = 0.2*(it+1); /* nag_pde_parab_1d_fd_ode_remesh (d03ppc). * General system of parabolic PDEs, coupled DAEs, method of * lines, finite differences, remeshing, one space variable */ nag_pde_parab_1d_fd_ode_remesh(npde, m, &ts, tout, pdedef, bndary, uvinit, u, npts, x, ncode, d03pck, nxi, xi, neqn, rtol, atol, itol, Nag_TwoNorm, Nag_LinAlgFull, algopt, remesh, nxfix, xfix, nrmesh, dxmesh, trmesh, ipminf, xratio, con, monitf, rsave, lrsave, isave, lisave, itask, itrace, 0, &ind, &comm, &saved, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_pde_parab_1d_fd_ode_remesh (d03ppc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Set output points */ switch (it) { case 0: for (i = 0; i < 5; ++i) xout[i] = 0.3+0.1*i; break; case 1: for (i = 0; i < 5; ++i) xout[i] = 0.4+0.1*i; break; case 2: for (i = 0; i < 5; ++i) xout[i] = 0.6+0.05*i; break; case 3: for (i = 0; i < 5; ++i) xout[i] = 0.7+0.05*i; break; case 4: for (i = 0; i < 5; ++i) xout[i] = 0.8+0.05*i; break; } fprintf(fpout, " t = %6.3f\n", ts); fprintf(fpout, " x "); for (i = 0; i < 5; ++i) { fprintf(fpout, "%9.4f", xout[i]); fprintf(fpout, (i+1)%5 == 0 || i == 4?"\n":" "); } /* Interpolate at output points */ /* nag_pde_interp_1d_fd (d03pzc). PDEs, spatial interpolation with * nag_pde_parab_1d_fd_ode_remesh (d03ppc), */ nag_pde_interp_1d_fd(npde, m, u, npts, x, xout, intpts, itype, uout, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_pde_interp_1d_fd (d03pzc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Check against exact solution */ exact(ts, xout, intpts, ue, &comm); fprintf(fpout, " Approx sol. "); for (i = 1; i <= intpts; ++i) { fprintf(fpout, "%9.4f", UOUT(1, i, 1)); fprintf(fpout, i%5 == 0 || i == 5?"\n":" "); } fprintf(fpout, " Exact sol. "); for (i = 1; i <= intpts; ++i) { fprintf(fpout, "%9.4f", ue[i-1]); fprintf(fpout, i%5 == 0 || i == 5?"\n":" "); } fprintf(fpout, "\n"); } fprintf(fpout, " Number of integration steps in time = %6ld\n", isave[0]); fprintf(fpout, " Number of function evaluations = %6ld\n", isave[1]); fprintf(fpout, " Number of Jacobian evaluations = %6ld\n", isave[2]); fprintf(fpout, " Number of iterations = %6ld\n\n", isave[4]); END: if (fpout != stdout) fclose(fpout); if (algopt) NAG_FREE(algopt); if (atol) NAG_FREE(atol); if (rsave) NAG_FREE(rsave); if (rtol) NAG_FREE(rtol); if (u) NAG_FREE(u); if (ue) NAG_FREE(ue); if (uout) NAG_FREE(uout); if (x) NAG_FREE(x); if (xfix) NAG_FREE(xfix); if (xi) NAG_FREE(xi); if (xout) NAG_FREE(xout); if (isave) NAG_FREE(isave); return exit_status; } static void NAG_CALL uvinit(Integer npde, Integer npts, Integer nxi, const double x[], const double xi[], double u[], Integer ncode, double v[], Nag_Comm *comm) { double *e = (double *) comm->p; double a, b, c, t; Integer i; t = 0.0; for (i = 1; i <= npts; ++i) { a = (x[i-1] - 0.25 - 0.75*t)/(*e*4.0); b = (0.9*x[i-1] - 0.325 - 0.495*t)/(*e*2.0); if (a > 0.0 && a > b) { a = exp(-a); c = (0.8*x[i-1] - 0.4 - 0.24*t)/(*e*4.0); c = exp(c); U(1, i) = (0.1*c + 0.5 + a)/(c + 1.0 + a); } else if (b > 0.0 && b >= a) { b = exp(-b); c = (-0.8*x[i-1] + 0.4 + 0.24*t)/(*e*4.0); c = exp(c); U(1, i) = (0.5*c + 0.1 + b)/(c + 1.0 + b); } else { a = exp(a); b = exp(b); U(1, i) = (0.5*a + 1.0 + 0.1*b)/(a + 1.0 + b); } } return; } static void NAG_CALL pdedef(Integer npde, double t, double x, const double u[], const double ux[], Integer ncode, const double v[], const double vdot[], double p[], double q[], double r[], Integer *ires, Nag_Comm *comm) { double *e = (double *) comm->p; P(1, 1) = 1.0; r[0] = *e*ux[0]; q[0] = u[0]*ux[0]; return; } static void NAG_CALL bndary(Integer npde, double t, const double u[], const double ux[], Integer ncode, const double v[], const double vdot[], Integer ibnd, double beta[], double gamma[], Integer *ires, Nag_Comm *comm) { double a, b, c, ue, x; double *e = (double *) comm->p; beta[0] = 0.0; if (ibnd == 0) { x = 0.0; a = (x - 0.25 - 0.75*t)/(*e*4.0); b = (0.9*x - 0.325 - 0.495*t)/(*e*2.0); if (a > 0. && a > b) { a = exp(-a); c = (0.8*x - 0.4 - 0.24*t)/(*e*4.0); c = exp(c); ue = (0.1*c + 0.5 + a)/(c + 1.0 + a); } else if (b > 0.0 && b >= a) { b = exp(-b); c = (-0.8*x + 0.4 + 0.24*t)/(*e*4.0); c = exp(c); ue = (0.5*c + 0.1 + b)/(c + 1.0 + b); } else { a = exp(a); b = exp(b); ue = (0.5*a + 1.0 + 0.1*b)/(a + 1.0 + b); } } else { x = 1.0; a = (x - 0.25 - 0.75*t)/(*e*4.0); b = (0.9*x - 0.325 - 0.495*t)/(*e*2.0); if (a > 0.0 && a > b) { a = exp(-a); c = (0.8*x - 0.4 - 0.24*t)/(*e*4.0); c = exp(c); ue = (0.1*c + 0.5 + a)/(c + 1.0 + a); } else if (b > 0.0 && b >= a) { b = exp(-b); c = (-0.8*x + 0.4 + 0.24*t)/(*e*4.0); c = exp(c); ue = (0.5*c + 0.1 + b)/(c + 1.0 + b); } else { a = exp(a); b = exp(b); ue = (0.5*a + 1.0 + 0.1*b)/(a + 1.0 + b); } } gamma[0] = u[0] - ue; return; } static void NAG_CALL exact(double t, double *x, Integer npts, double *u, Nag_Comm *comm) { /* Exact solution (for comparison purposes) */ double a, b, c; double *e = (double *) comm->p; Integer i; for (i = 0; i < npts; ++i) { a = (x[i] - 0.25 - 0.75*t)/(*e*4.0); b = (0.9*x[i] - 0.325 - 0.495* t)/(*e*2.0); if (a > 0. && a > b) { a = exp(-a); c = (0.8*x[i] - 0.4 - 0.24* t)/(*e*4.0); c = exp(c); u[i] = (0.1*c + 0.5 + a)/ (c + 1.0 + a); } else if (b > 0. && b >= a) { b = exp(-b); c = (-0.8*x[i] + 0.4 + 0.24* t)/(*e*4.0); c = exp(c); u[i] = (0.5*c + 0.1 + b)/ (c + 1.0 + b); } else { a = exp(a); b = exp(b); u[i] = (0.5*a + 1.0 + 0.1* b)/(a + 1.0 + b); } } return; } static void NAG_CALL monitf(double t, Integer npts, Integer npde, const double x[], const double u[], const double r[], double fmon[], Nag_Comm *comm) { double drdx, h; Integer i, k, l; for (i = 1; i <= npts-1; ++i) { k = i-1; if (i == 1) k = 1; l = i+1; h = 0.5* (x[l- 1] - x[k-1]); /* Second derivative */ drdx = (R(1, i+ 1) - R(1, i))/h; fmon [i-1] = drdx; if (fmon[i-1] < 0) fmon[i-1] = -drdx; } fmon [npts- 1] = fmon[npts-2]; return; }