/* nag_sparse_sym_basic_setup (f11gdc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf11.h>
int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  double anorm, dscale, dtol, sigerr, sigmax, sigtol, stplhs, stprhs, tol;
  Integer i, irevcm, iterm, itn, its, la, lfill,
         lwork, lwreq, maxitn, maxits, monit, n, nnz, nnzc, npivm;
  /* Arrays */
  char nag_enum_arg[100];
  double *a = 0, *b = 0, *wgt = 0, *work = 0, *x = 0;
  Integer *icol = 0, *ipiv = 0, *irow = 0, *istr = 0;
  /* NAG types */
  Nag_SparseSym_Piv pstrat;
  Nag_SparseSym_Method method;
  Nag_SparseSym_PrecType precon;
  Nag_NormType norm;
  Nag_SparseSym_Weight weight;
  Nag_SparseSym_Fact mic;
  Nag_Sparse_Comm comm;
  Nag_SparseSym_Bisection sigcmp;
  NagError fail, fail1;

  INIT_FAIL(fail);
  INIT_FAIL(fail1);

  printf("nag_sparse_sym_basic_setup (f11gdc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);
  /* Read or initialize the parameters for the iterative solver */
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  scanf("%99s%*[^\n]", nag_enum_arg);
  method = (Nag_SparseSym_Method) nag_enum_name_to_value(nag_enum_arg);
  scanf("%99s%*[^\n]", nag_enum_arg);
  precon = (Nag_SparseSym_PrecType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%99s%*[^\n]", nag_enum_arg);
  sigcmp = (Nag_SparseSym_Bisection) nag_enum_name_to_value(nag_enum_arg);
  scanf("%99s%*[^\n]", nag_enum_arg);
  norm = (Nag_NormType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%99s%*[^\n]", nag_enum_arg);
  weight = (Nag_SparseSym_Weight) nag_enum_name_to_value(nag_enum_arg);
  scanf("%" NAG_IFMT "%*[^\n]", &iterm);
  scanf("%lf%" NAG_IFMT "%*[^\n]", &tol, &maxitn);
  scanf("%" NAG_IFMT "%*[^\n] ", &monit);

  /* Read the parameters for the preconditioner */
  scanf("%" NAG_IFMT "%lf%*[^\n]", &lfill, &dtol);
  scanf("%99s%*[^\n]", nag_enum_arg);
  mic = (Nag_SparseSym_Fact) nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%*[^\n]", &dscale);
  scanf("%99s%*[^\n]", nag_enum_arg);
  pstrat = (Nag_SparseSym_Piv) nag_enum_name_to_value(nag_enum_arg);

  la = 2 * nnz;
  lwork = 120;
  if (!(a = NAG_ALLOC(la, double)) ||
      !(b = NAG_ALLOC(n, double)) ||
      !(wgt = NAG_ALLOC(n, double)) ||
      !(work = NAG_ALLOC(lwork, double)) ||
      !(x = NAG_ALLOC(n, double)) ||
      !(icol = NAG_ALLOC(la, Integer)) ||
      !(ipiv = NAG_ALLOC(n, Integer)) ||
      !(irow = NAG_ALLOC(la, Integer)) || !(istr = NAG_ALLOC(n + 1, Integer))
         )
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read the nonzero elements of the matrix A */
  for (i = 0; i < nnz; i++)
    scanf("%lf%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i], &irow[i], &icol[i]);

  /* Read right-hand side vector b and initial approximate solution x */
  for (i = 0; i < n; i++)
    scanf("%lf", &b[i]);
  scanf("%*[^\n]");
  for (i = 0; i < n; i++)
    scanf("%lf", &x[i]);

  printf("\nSolve a system of linear equations using the ");
  if (method == Nag_SparseSym_CG)
    printf("conjugate gradient method (CG)\n");
  else if (method == Nag_SparseSym_SYMMLQ)
    printf("Lanczos method (SYMMLQ)\n");
  else if (method == Nag_SparseSym_MINRES)
    printf("minimum residual method (MINRES)\n");

  /* Calculate incomplete Cholesky factorization as preconditioner using
   * nag_sparse_sym_chol_fac (f11jac).
   * Incomplete Cholesky factorization (symmetric)
   */
  nag_sparse_sym_chol_fac(n, nnz, &a, &la, &irow, &icol, lfill, dtol, mic,
                          dscale, pstrat, ipiv, istr, &nnzc, &npivm, &comm,
                          &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_sym_chol_fac (f11jac)\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Initialize the solver using nag_sparse_sym_basic_setup (f11gdc).
   * Real sparse symmetric linear systems, setup for f11gec
   */
  anorm = 0.0;
  sigmax = 0.0;
  sigtol = 0.01;
  maxits = n;
  while (1) {
    nag_sparse_sym_basic_setup(method, precon, sigcmp, norm, weight, iterm, n,
                               tol, maxitn, anorm, sigmax, sigtol, maxits,
                               monit, &lwreq, work, lwork, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_sparse_sym_basic_setup (f11gdc)\n%s\n",
             fail.message);
      exit_status = 2;
      goto END;
    }
    if (lwork >= lwreq)
      break;
    else {
      NAG_FREE(work);
      lwork = lwreq;
      work = NAG_ALLOC(lwreq, double);
    }
  }

  /* Call repeatedly to solve the equations
   * Note that the arrays b and x are overwritten
   * On final exit, x will contain the solution and b the residual vector
   */
  irevcm = 0;
  /* First call of nag_sparse_sym_basic_solver (f11gec).
   * Real sparse symmetric linear systems, preconditioned conjugate 
   * gradient or Lanczos
   */
  nag_sparse_sym_basic_solver(&irevcm, x, b, wgt, work, lwork, &fail);
  while (irevcm != 4) {
    switch (irevcm) {
    case 1:
      /* nag_sparse_sym_matvec (f11xec)
       * Real sparse symmetric matrix vector multiply 
       */
      nag_sparse_sym_matvec(n, nnz, a, irow, icol, Nag_SparseSym_NoCheck,
                            x, b, &fail1);
      break;
    case 2:
      /* nag_sparse_sym_precon_ichol_solve (f11jbc).
       * Solution of linear system involving incomplete Cholesky 
       * preconditioning matrix generated by f11jac
       */
      nag_sparse_sym_precon_ichol_solve(n, a, la, irow, icol, ipiv, istr,
                                        Nag_SparseSym_NoCheck, x, b, &fail1);
      break;
    case 3:
      /* nag_sparse_sym_basic_diagnostic (f11gfc).
       * Real sparse symmetric linear systems, diagnostic for f11gec
       */
      nag_sparse_sym_basic_diagnostic(&itn, &stplhs, &stprhs, &anorm,
                                      &sigmax, &its, &sigerr, work, lwork,
                                      &fail1);
      printf("\nMonitoring at iteration no.%4" NAG_IFMT "\n", itn);
      printf("residual norm: %14.4e\n", stplhs);
      printf("  Solution vector  Residual vector\n");
      for (i = 0; i < n; i++)
        printf("%16.4e%16.4e\n", x[i], b[i]);
      printf("\n");
    }
    if (fail1.code != NE_NOERROR)
      irevcm = 6;
    /* Next call of nag_sparse_sym_basic_solver (f11gec). */
    nag_sparse_sym_basic_solver(&irevcm, x, b, wgt, work, lwork, &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_sym_basic_solver (f11gec).\n%s\n",
           fail.message);
    exit_status = 3;
    goto END;
  }

  /* Obtain information about the computation using 
   * nag_sparse_sym_basic_diagnostic (f11gfc).
   * Real sparse symmetric linear systems, diagnostic for solver.
   */
  nag_sparse_sym_basic_diagnostic(&itn, &stplhs, &stprhs, &anorm, &sigmax,
                                  &its, &sigerr, work, lwork, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_sym_basic_diagnostic (f11gfc)\n%s\n",
           fail.message);
    exit_status = 4;
    goto END;
  }
  /* Print the output data */
  printf("Final Results\n");
  printf("Number of iterations for convergence:     %5" NAG_IFMT " \n", itn);
  printf("Residual norm:                            %14.4e\n", stplhs);
  printf("Right-hand side of termination criterion: %14.4e\n", stprhs);
  printf("1-norm of matrix A:                       %14.4e\n", anorm);
  printf("Largest singular value of A_bar:          %14.4e\n\n", sigmax);
  /* Output x */
  printf("%14s%14s\n", "Solution", "Residual");
  for (i = 0; i < n; i++)
    printf("%14.4e%14.4e\n", x[i], b[i]);
  printf("\n");

END:

  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(wgt);
  NAG_FREE(work);
  NAG_FREE(x);
  NAG_FREE(icol);
  NAG_FREE(ipiv);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}