NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_sparseig_real_init (f12aac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

#include <nag.h>
#include <stdio.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_sparseig_real_init (f12aac) Example Program "
         "Results\n");
  /* Skip heading in data file. */
  scanf("%*[^\n] ");

  /* Read values for nx, nev and cnv from data file. */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\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;
  }

  /* Initialize communication arrays for problem using
     nag_sparseig_real_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_sparseig_real_init(n, nev, ncv, icomm, licomm, comm, lcomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_real_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_sparseig_real_init(n, nev, ncv, icomm, licomm, comm, lcomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_real_init (f12aac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Select the required spectrum using
     nag_sparseig_real_option (f12adc). */
  nag_sparseig_real_option("SMALLEST MAG", icomm, comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_real_option (f12adc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  irevcm = 0;
REVCOMLOOP:
  /* Repeated calls to reverse communication routine
     nag_sparseig_real_iter (f12abc). */
  nag_sparseig_real_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_sparseig_real_monit (f12aec). */
      nag_sparseig_real_monit(&niter, &nconv, eigvr, eigvi, eigest, icomm,
                              comm);
      /* Compute 2-norm of Ritz estimates using
         nag_blast_dge_norm (f16rac). */
      nag_blast_dge_norm(Nag_ColMajor, Nag_FrobeniusNorm, nev, 1, eigest, nev,
                         &estnrm, &fail);
      printf("Iteration %3" NAG_IFMT ", ", niter);
      printf(" No. converged = %3" NAG_IFMT ",", nconv);
      printf(" norm of estimates = %17.8e\n", estnrm);
    }
    goto REVCOMLOOP;
  }
  if (fail.code == NE_NOERROR) {
    /* Post-Process using nag_sparseig_real_proc
       (f12acc) to compute eigenvalues/vectors. */
    nag_sparseig_real_proc(&nconv, eigvr, eigvi, v, sigmar, sigmai, resid, v,
                           comm, icomm, &fail);
    printf("\n\n The %4" NAG_IFMT " Ritz values", nconv);
    printf(" of smallest magnitude are:\n\n");
    for (j = 0; j <= nconv - 1; ++j) {
      printf("%8" NAG_IFMT "%5s( %12.4f , %12.4f )\n", j + 1, "", eigvr[j],
             eigvi[j]);
    }
  } else {
    printf("Error from nag_sparseig_real_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_blast_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_blast_daxpby(nx, nx2, &v[lo - nx], 1, beta, &w[lo], 1, &fail);
    nag_blast_daxpby(nx, nx2, &v[lo + nx], 1, beta, &w[lo], 1, &fail);
  }
  lo = (nx - 1) * nx;
  tv(nx, &v[lo], &w[lo]);
  nag_blast_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 */