/* nag_sparse_nsym_fac_solve (f11dcc) Example Program.
*
* Copyright 2020 Numerical Algorithms Group.
*
* Mark 27.1, 2020.
*
*/
#include <nag.h>
#include <stdio.h>
int main(void) {
double dtol;
double *a = 0, *b = 0;
double *x = 0;
double rnorm;
double tol;
Integer exit_status = 0;
Integer *irow, *icol;
Integer *istr = 0, *idiag, *ipivp = 0, *ipivq = 0;
Integer i, m, n, nnzc;
Integer lfill, npivm;
Integer maxitn;
Integer itn;
Integer nnz;
Integer num;
char nag_enum_arg[40];
Nag_SparseNsym_Method method;
Nag_SparseNsym_Piv pstrat;
Nag_SparseNsym_Fact milu;
Nag_Sparse_Comm comm;
NagError fail;
INIT_FAIL(fail);
printf("nag_sparse_real_gen_solve_ilu (f11dcc) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%*[^\n]", &n);
scanf("%" NAG_IFMT "%*[^\n]", &nnz);
scanf("%39s%*[^\n]", nag_enum_arg);
/* nag_enum_name_to_value (x04nac).
* Converts NAG enum member name to value
*/
method = (Nag_SparseNsym_Method)nag_enum_name_to_value(nag_enum_arg);
scanf("%" NAG_IFMT "%lf%*[^\n]", &lfill, &dtol);
scanf("%39s%*[^\n]", nag_enum_arg);
pstrat = (Nag_SparseNsym_Piv)nag_enum_name_to_value(nag_enum_arg);
scanf("%39s%*[^\n]", nag_enum_arg);
milu = (Nag_SparseNsym_Fact)nag_enum_name_to_value(nag_enum_arg);
scanf("%" NAG_IFMT "%lf%" NAG_IFMT "%*[^\n]", &m, &tol, &maxitn);
/* Read the matrix a */
num = 2 * nnz;
istr = NAG_ALLOC(n + 1, Integer);
idiag = NAG_ALLOC(n, Integer);
ipivp = NAG_ALLOC(n, Integer);
ipivq = NAG_ALLOC(n, Integer);
x = NAG_ALLOC(n, double);
b = NAG_ALLOC(n, double);
a = NAG_ALLOC(num, double);
irow = NAG_ALLOC(num, Integer);
icol = NAG_ALLOC(num, Integer);
if (!istr || !idiag || !ipivp || !ipivq || !irow || !icol || !a || !x || !b) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i = 1; i <= nnz; ++i)
scanf("%lf%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i - 1], &irow[i - 1],
&icol[i - 1]);
/* Read right-hand side vector b and initial approximate solution x */
for (i = 1; i <= n; ++i)
scanf("%lf", &b[i - 1]);
scanf("%*[^\n]");
for (i = 1; i <= n; ++i)
scanf("%lf", &x[i - 1]);
scanf("%*[^\n]");
/* Calculate incomplete LU factorization */
/* nag_sparse_real_gen_precon_ilu (f11dac).
* Incomplete LU factorization (nonsymmetric)
*/
nag_sparse_real_gen_precon_ilu(n, nnz, &a, &num, &irow, &icol, lfill, dtol,
pstrat, milu, ipivp, ipivq, istr, idiag, &nnzc,
&npivm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparse_real_gen_precon_ilu (f11dac).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* nag_sparse_real_gen_solve_ilu (f11dcc).
* Solver with incomplete LU preconditioning (nonsymmetric)
*/
/* Solve Ax = b using nag_sparse_real_gen_solve_ilu (f11dcc) */
nag_sparse_real_gen_solve_ilu(method, n, nnz, a, num, irow, icol, ipivp,
ipivq, istr, idiag, b, m, tol, maxitn, x,
&rnorm, &itn, &comm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparse_real_gen_solve_ilu (f11dcc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
printf("%s%10" NAG_IFMT "%s\n", "Converged in", itn, " iterations");
printf("%s%16.3e\n", "Final residual norm =", rnorm);
/* Output x */
printf(" x\n");
for (i = 1; i <= n; ++i)
printf(" %16.6e\n", x[i - 1]);
END:
NAG_FREE(istr);
NAG_FREE(idiag);
NAG_FREE(ipivp);
NAG_FREE(ipivq);
NAG_FREE(irow);
NAG_FREE(icol);
NAG_FREE(a);
NAG_FREE(x);
NAG_FREE(b);
return exit_status;
}