/* nag_real_symm_sparse_eigensystem_init (f12fac) Example Program. * * Copyright 2005 Numerical Algorithms Group. * * Mark 8, 2005. */ #include #include #include #include #include #include #include static void tv(Integer, double *, double *); static void av(Integer, double *, double *, FILE *fpout); int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Constants */ Integer licomm = 140, imon = 0; /* Scalars */ double sigma = 0, estnrm; Integer exit_status, irevcm, j, lcomm, n, nconv, ncv, nev; Integer niter, nshift, nx; /* Nag types */ NagError fail; /* Arrays */ double *comm = 0, *eigv = 0, *eigest = 0; double *resid = 0, *v = 0; Integer *icomm = 0; /* Ponters */ double *mx = 0, *x = 0, *y = 0; exit_status = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_real_symm_sparse_eigensystem_init (f12fac) Example " "Program Results\n"); /* Skip heading in data file. */ fscanf(fpin, "%*[^\n] "); /* Read values for nx, nev and cnv from data file. */ fscanf(fpin, "%ld%ld%ld%*[^\n] ", &nx, &nev, &ncv); /* Allocate memory */ n = nx * nx; lcomm = 3*n + ncv*ncv + 8*ncv + 60; if (!(comm = NAG_ALLOC(lcomm, double)) || !(eigv = NAG_ALLOC(ncv, double)) || !(eigest = NAG_ALLOC(ncv, double)) || !(resid = NAG_ALLOC(n, double)) || !(v = NAG_ALLOC(n * ncv, double)) || !(icomm = NAG_ALLOC(licomm, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Initialise communication arrays for problem using nag_real_symm_sparse_eigensystem_init (f12fac). */ nag_real_symm_sparse_eigensystem_init(n, nev, ncv, icomm, licomm, comm, lcomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from " "nag_real_symm_sparse_eigensystem_init (f12fac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Select the required spectrum using nag_real_symm_sparse_eigensystem_option (f12fdc). */ nag_real_symm_sparse_eigensystem_option("smallest magnitude", icomm, comm, &fail); if (fail.code != NE_NOERROR) { fprintf( fpout, "Error from nag_real_symm_sparse_eigensystem_option (f12fdc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Increase the iteration limit if required. */ nag_real_symm_sparse_eigensystem_option("iteration limit=500", icomm, comm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_symm_sparse_eigensystem_option " "(f12fdc).\n%s\n", fail.message); exit_status = 1; goto END; } irevcm = 0; REVCOMLOOP: /* Repeated calls to reverse communication routine nag_real_symm_sparse_eigensystem_iter (f12fbc). */ nag_real_symm_sparse_eigensystem_iter(&irevcm, resid, v, &x, &y, &mx, &nshift, comm, icomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_symm_sparse_eigensystem_iter " "(f12fbc).\n%s\n", fail.message); exit_status = 1; goto END; } if (irevcm != 5) { if (irevcm == -1 || irevcm == 1) { /* Perform matrix vector multiplication y <--- Op*x */ av(nx, x, y, fpout); } else if (irevcm == 4 && imon == 1) { /* If imon=1, get monitoring information using nag_real_symm_sparse_eigensystem_monit (f12fec). */ nag_real_symm_sparse_eigensystem_monit(&niter, &nconv, eigv, eigest, icomm, comm); /* Compute 2-norm of Ritz estimates using nag_dge_norm (f16rac).*/ nag_dge_norm(Nag_ColMajor, Nag_FrobeniusNorm, nev, 1, eigest, nev, &estnrm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dge_norm (f16rac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Iteration %3ld, ", niter); fprintf(fpout, " No. converged = %3ld,", nconv); fprintf(fpout, " norm of estimates = %17.8e\n", estnrm); } goto REVCOMLOOP; } if (fail.code == NE_NOERROR) { /* Post-Process using nag_real_symm_sparse_eigensystem_sol (f12fcc) to compute eigenvalues/vectors. */ nag_real_symm_sparse_eigensystem_sol(&nconv, eigv, v, sigma, resid, v, comm, icomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_symm_sparse_eigensystem_sol " "(f12fcc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n\n The %4ld Ritz values", nconv); fprintf(fpout, " of smallest magnitude are:\n\n"); for (j = 0; j <= nconv-1; ++j) { fprintf(fpout, "%8ld%5s%12.4f\n", j+1, "", eigv[j]); } } else { fprintf(fpout, " Error from nag_real_symm_sparse_eigensystem_iter " "(f12fbc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (comm) NAG_FREE(comm); if (eigv) NAG_FREE(eigv); if (eigest) NAG_FREE(eigest); if (resid) NAG_FREE(resid); if (v) NAG_FREE(v); if (icomm) NAG_FREE(icomm); return exit_status; } static void av(Integer nx, double *v, double *w, FILE *fpout) { /* Scalars */ double nx2; Integer j, lo; /* Nag types */ NagError fail; /* Function Body */ INIT_FAIL(fail); nx2 = ((double)((nx + 1) * (nx + 1))); tv(nx, v, w); nag_daxpby(nx, -nx2, &v[nx], 1, nx2, w, 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_daxpby (f16ecc).\n%s\n", fail.message); goto END; } for (j = 1; j <= nx - 2; ++j) { lo = j * nx; tv(nx, &v[lo], &w[lo]); nag_daxpby(nx, -nx2, &v[lo-nx], 1, nx2, &w[lo], 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_daxpby (f16ecc).\n%s\n", fail.message); goto END; } nag_daxpby(nx, -nx2, &v[lo+nx], 1, 1.0, &w[lo], 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_daxpby (f16ecc).\n%s\n", fail.message); goto END; } } lo = (nx - 1) * nx; tv(nx, &v[lo], &w[lo]); nag_daxpby(nx, -nx2, &v[lo-nx], 1, nx2, &w[lo], 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_daxpby (f16ecc).\n%s\n", fail.message); goto END; } END: return; } /* av */ static void tv(Integer nx, double *x, double *y) { /* Compute the matrix vector multiplication y<---T*x where T is a nx */ /* by nx tridiagonal matrix with constant diagonals (dd, dl and du). */ /* Scalars */ double dd, dl, du; Integer j; /* Function Body */ dd = 4.0; dl = -1.0; du = -1.0; y[0] = dd * x[0] + du * x[1]; for (j = 1; j <= nx - 2; ++j) { y[j] = dl * x[j-1] + dd * x[j] + du * x[j+1]; } y[nx-1] = dl * x[nx-2] + dd * x[nx-1]; return; } /* tv */