/* nag_sparse_nsym_precon_ilu_solve (f11dbc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf11.h>
int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  double dtol;
  Integer i, la, lfill, n, nnz, nnzc, npivm;
  /* Arrays */
  double *a = 0, *x = 0, *y = 0;
  Integer *icol = 0, *idiag = 0, *ipivp = 0, *ipivq = 0, *irow = 0, *istr = 0;
  /* NAG types */
  Nag_SparseNsym_Piv pstrat;
  Nag_SparseNsym_Fact milu;
  Nag_SparseNsym_CheckData check;
  Nag_TransType trans;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nsym_precon_ilu_solve (f11dbc) Example Program Results");
  printf("\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);
  la = 2 * nnz;
  if (!(a = NAG_ALLOC((la), double)) ||
      !(x = NAG_ALLOC((n), double)) ||
      !(y = NAG_ALLOC((n), double)) ||
      !(icol = NAG_ALLOC((la), Integer)) ||
      !(idiag = NAG_ALLOC((n), Integer)) ||
      !(ipivp = NAG_ALLOC((n), Integer)) ||
      !(ipivq = 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 the vector y */
  for (i = 0; i < n; i++)
    scanf("%lf", &y[i]);

  /* Calculate LU factorization */
  lfill = -1;
  dtol = 0.0;
  pstrat = Nag_SparseNsym_CompletePiv;
  milu = Nag_SparseNsym_UnModFact;
  /* nag_sparse_nsym_fac (f11dac).
   * Incomplete LU factorization (nonsymmetric)
   */
  nag_sparse_nsym_fac(n, nnz, &a, &la, &irow, &icol, lfill, dtol, pstrat,
                      milu, ipivp, ipivq, istr, idiag, &nnzc, &npivm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nsym_fac (f11dac)\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Check value of npivm */
  if (npivm > 0) {
    printf("Factorization is not complete \n");
    goto END;
  }
  /* Solve P L D U x = y */
  check = Nag_SparseNsym_Check;
  trans = Nag_NoTrans;
  /* nag_sparse_nsym_precon_ilu_solve (f11dbc)
   * Solution of linear system involving incomplete LU preconditioning matrix
   */
  nag_sparse_nsym_precon_ilu_solve(trans, n, a, la, irow, icol, ipivp, ipivq,
                                   istr, idiag, check, y, x, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nsym_precon_ilu_solve (f11dbc)\n%s\n",
           fail.message);
    exit_status = 2;
    goto END;
  }
  /* Output results */
  printf(" Solution of linear system \n");
  for (i = 0; i < n; i++)
    printf("%16.4e\n", x[i]);
END:
  NAG_FREE(a);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(icol);
  NAG_FREE(idiag);
  NAG_FREE(ipivp);
  NAG_FREE(ipivq);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}