/* nag_real_sparse_eigensystem_init (f12aac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2005.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <nag_string.h>
#include <stdio.h>
#include <nagf12.h>
#include <nagf16.h>

static void tv(Integer, double *, double *);
static void av(Integer, double *, double *);

int main(void)
{
  /* Constants */
  Integer  imon = 0;
  /* Scalars */
  double   sigmai = 0, sigmar = 0, estnrm;
  Integer  exit_status, irevcm, j, lcomm, licomm, n, nconv, ncv, nev;
  Integer  niter, nshift, nx;
  /* Arrays */
  double   *comm = 0, *eigvr = 0, *eigvi = 0, *eigest = 0;
  double   *resid = 0, *v = 0;
  Integer  *icomm = 0;
  /* Pointers */
  double   *mx = 0, *x = 0, *y = 0;
  /* Nag types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_real_sparse_eigensystem_init (f12aac) Example Program "
          "Results\n");
  /* Skip heading in data file. */
  scanf("%*[^\n] ");

  /* Read values for nx, nev and cnv from data file. */
  scanf("%ld%ld%ld%*[^\n] ", &nx, &nev, &ncv);

  /* Allocate memory */
  n = nx * nx;
  if (!(eigvr = NAG_ALLOC(ncv, double)) ||
      !(eigvi = NAG_ALLOC(ncv, double)) ||
      !(eigest = NAG_ALLOC(ncv, double)) ||
      !(resid = NAG_ALLOC(n, double)) ||
      !(v = NAG_ALLOC(n * ncv, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  /* Initialise communication arrays for problem using
     nag_real_sparse_eigensystem_init (f12aac).
     The first call sets lcomm = licomm = -1 to perform a workspace
     query. */
  lcomm = licomm = -1;
  if (!(comm = NAG_ALLOC(1, double)) ||
      !(icomm = NAG_ALLOC(1, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  nag_real_sparse_eigensystem_init(n, nev, ncv, icomm, licomm,
                                   comm, lcomm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_real_sparse_eigensystem_init (f12aac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  lcomm = (Integer)comm[0];
  licomm = icomm[0];
  NAG_FREE(comm);
  NAG_FREE(icomm);
  if (!(comm = NAG_ALLOC(lcomm, double)) ||
      !(icomm = NAG_ALLOC(licomm, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  nag_real_sparse_eigensystem_init(n, nev, ncv, icomm, licomm,
                                   comm, lcomm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_real_sparse_eigensystem_init (f12aac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  /* Select the required spectrum using
     nag_real_sparse_eigensystem_option (f12adc). */
  nag_real_sparse_eigensystem_option("SMALLEST MAG", icomm,
                                     comm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_real_sparse_eigensystem_option (f12adc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  irevcm = 0;
 REVCOMLOOP:
  /* Repeated calls to reverse communication routine
     nag_real_sparse_eigensystem_iter (f12abc). */
  nag_real_sparse_eigensystem_iter(&irevcm, resid, v, &x, &y, &mx,
                                   &nshift, comm, icomm, &fail);
  if (irevcm != 5)
    {
      if (irevcm == -1 || irevcm == 1)
        {
          /* Perform matrix vector multiplication y <--- Op*x */
          av(nx, x, y);
        }
      else if (irevcm == 4 && imon == 1)
        {
          /* If imon=1, get monitoring information using
             nag_real_sparse_eigensystem_monit (f12aec). */
          nag_real_sparse_eigensystem_monit(&niter, &nconv, eigvr,
                                            eigvi, 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);
          printf("Iteration %3ld, ", niter);
          printf(" No. converged = %3ld,", nconv);
          printf(" norm of estimates = %17.8e\n", estnrm);
        }
      goto REVCOMLOOP;
    }
  if (fail.code == NE_NOERROR)
    {
      /* Post-Process using nag_real_sparse_eigensystem_sol
         (f12acc) to compute eigenvalues/vectors. */
      nag_real_sparse_eigensystem_sol(&nconv, eigvr, eigvi, v, sigmar,
                                      sigmai, resid, v, comm, icomm,
                                      &fail);
      printf("\n\n The %4ld Ritz values", nconv);
      printf(" of smallest magnitude are:\n\n");
      for (j = 0; j <= nconv-1; ++j)
        {
          printf("%8ld%5s( %12.4f , %12.4f )\n", j+1, "",
                  eigvr[j], eigvi[j]);
        }
    }
  else
    {
      printf("Error from nag_real_sparse_eigensystem_iter (f12abc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
 END:
  NAG_FREE(comm);
  NAG_FREE(eigvr);
  NAG_FREE(eigvi);
  NAG_FREE(eigest);
  NAG_FREE(resid);
  NAG_FREE(v);
  NAG_FREE(icomm);
  return exit_status;
}

static void av(Integer nx, double *v, double *w)
{
  /* Constants*/
  const double beta = 1.0;
  /* 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, beta, w, 1, &fail);
  for (j = 2; j <= nx - 1; ++j)
    {
      lo = (j - 1) * nx;
      tv(nx, &v[lo], &w[lo]);
      nag_daxpby(nx, nx2, &v[lo-nx], 1, beta, &w[lo], 1, &fail);
      nag_daxpby(nx, nx2, &v[lo+nx], 1, beta, &w[lo], 1, &fail);
    }
  lo = (nx - 1) * nx;
  tv(nx, &v[lo], &w[lo]);
  nag_daxpby(nx, nx2, &v[lo-nx], 1, beta, &w[lo], 1, &fail);
  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, nx1, nx2;
  Integer j;
  /* Function Body */
  nx1 = (double)(nx + 1);
  nx2 = nx1 * nx1;
  dd = nx2 * 4.;
  dl = -nx2 - nx1 * 50.;
  du = -nx2 + nx1 * 50.;
  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 */