/* nag_sparse_complex_gen_precon_ssor_solve (f11drc) Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
*
* Mark 27.0, 2019.
*/
#include <nag.h>
int main(void)
{
/* Scalars */
Integer exit_status = 0;
double anorm, omega, sigmax, stplhs, stprhs, tol;
Integer i, irevcm, iterm, itn, liwork, lwneed, lwork, m,
maxitn, monit, n, nnz;
/* Arrays */
char nag_enum_arg[100];
Complex *a = 0, *b = 0, *rdiag = 0, *work = 0, *x = 0;
double *wgt = 0;
Integer *icol = 0, *irow = 0, *iwork = 0;
/* NAG types */
Nag_SparseNsym_CheckData ckdr, ckxn;
Nag_NormType norm;
Nag_SparseNsym_PrecType precon;
Nag_SparseNsym_Method method;
Nag_TransType trans;
Nag_SparseNsym_Weight weight;
NagError fail, fail1;
INIT_FAIL(fail);
INIT_FAIL(fail1);
printf("nag_sparse_complex_gen_precon_ssor_solve (f11drc) Example Program Results");
printf("\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
/* Read algorithmic parameters */
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &m);
scanf("%" NAG_IFMT "%*[^\n]", &nnz);
lwork = MAX(121 + n * (3 + m) + m * (m + 5), 120 + 7 * n);
liwork = 2 * n + 1;
if (!(a = NAG_ALLOC((nnz), Complex)) ||
!(b = NAG_ALLOC((n), Complex)) ||
!(rdiag = NAG_ALLOC((n), Complex)) ||
!(work = NAG_ALLOC((lwork), Complex)) ||
!(x = NAG_ALLOC((n), Complex)) ||
!(wgt = NAG_ALLOC((n), double)) ||
!(icol = NAG_ALLOC((nnz), Integer)) ||
!(irow = NAG_ALLOC((nnz), Integer)) ||
!(iwork = NAG_ALLOC((liwork), Integer))
)
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read or initialize the parameters for the iterative solver */
scanf("%99s%*[^\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("%99s%*[^\n]", nag_enum_arg);
precon = (Nag_SparseNsym_PrecType) 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("%" NAG_IFMT "%*[^\n]", &iterm);
scanf("%lf%" NAG_IFMT "%*[^\n]", &tol, &maxitn);
scanf("%lf%lf%*[^\n]", &anorm, &sigmax);
scanf("%lf%*[^\n]", &omega);
/* Read the matrix a */
for (i = 0; i < nnz; i++)
scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n]",
&a[i].re, &a[i].im, &irow[i], &icol[i]);
/* Read rhs vector b and initial approximate solution x */
for (i = 0; i < n; i++)
scanf(" ( %lf , %lf )", &b[i].re, &b[i].im);
scanf("%*[^\n]");
for (i = 0; i < n; i++)
scanf(" ( %lf , %lf )", &x[i].re, &x[i].im);
weight = Nag_SparseNsym_UnWeighted;
monit = 0;
/* Call to initialize solver */
/* nag_sparse_complex_gen_basic_setup (f11brc)
* Complex sparse non-Hermitian linear systems, setup
*/
nag_sparse_complex_gen_basic_setup(method, precon, norm, weight, iterm, n, m, tol,
maxitn, anorm, sigmax, monit, &lwneed, work,
lwork, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparse_complex_gen_basic_setup (f11brc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Calculate reciprocal diagonal matrix elements if necessary */
if (precon == Nag_SparseNsym_Prec) {
for (i = 0; i < n; i++)
iwork[i] = 0;
for (i = 0; i < nnz; i++) {
if (irow[i] == icol[i]) {
iwork[irow[i] - 1]++;
if (nag_complex_equal(a[i], nag_complex_create(0.0, 0.0))) {
printf("Matrix has a zero diagonal element\n");
goto END;
}
else {
rdiag[irow[i] - 1] =
nag_complex_divide(nag_complex_create(1.0, 0.0), a[i]);
}
}
}
for (i = 0; i < n; i++) {
if (iwork[i] == 0) {
printf("Matrix has a missing diagonal element\n");
goto END;
}
if (iwork[i] >= 2) {
printf("Matrix has a multiple diagonal element\n");
goto END;
}
}
}
/* Call solver repeatedly to solve the equations */
irevcm = 0;
ckxn = Nag_SparseNsym_Check;
ckdr = Nag_SparseNsym_Check;
while (irevcm != 4) {
/* nag_sparse_complex_gen_basic_solver (f11bsc).
* Complex sparse non-Hermitian linear systems, solver routine
* preconditioned RGMRES, CGS, Bi-CGSTAB or TFQMR method
*/
nag_sparse_complex_gen_basic_solver(&irevcm, x, b, wgt, work, lwork, &fail);
switch (irevcm) {
case 1:
/* Compute matrix-vector product */
trans = Nag_NoTrans;
/* nag_sparse_complex_gen_matvec (f11xnc).
* Complex sparse non-Hermitian matrix vector multiply
*/
nag_sparse_complex_gen_matvec(trans, n, nnz, a, irow, icol, ckxn, x, b,
&fail1);
ckxn = Nag_SparseNsym_NoCheck;
break;
case -1:
/* Compute conjugate transposed matrix-vector product */
trans = Nag_ConjTrans;
nag_sparse_complex_gen_matvec(trans, n, nnz, a, irow, icol, ckxn, x, b,
&fail1);
ckxn = Nag_SparseNsym_NoCheck;
break;
case 2:
/* SSOR preconditioning */
trans = Nag_NoTrans;
/* nag_sparse_complex_gen_precon_ssor_solve (f11drc).
* Solution of linear system involving preconditioning matrix generated
* by applying SSOR to complex sparse non-Hermitian matrix
*/
nag_sparse_complex_gen_precon_ssor_solve(trans, n, nnz, a, irow, icol, rdiag,
omega, ckdr, x, b, &fail1);
ckdr = Nag_SparseNsym_NoCheck;
break;
case 4:
/* Termination */
break;
default:
goto END;
}
if (fail1.code != NE_NOERROR) {
printf("Error from matrix-vector or preconditioning stage.\n%s\n",
fail1.message);
exit_status = 2;
goto END;
}
}
if (fail.code != NE_NOERROR) {
printf("Error from nag_sparse_complex_gen_basic_solver (f11bsc).\n%s\n",
fail.message);
exit_status = 3;
goto END;
}
/* nag_sparse_complex_gen_basic_diag (f11btc)
* Complex sparse non-Hermitian linear systems, diagnostic
*/
nag_sparse_complex_gen_basic_diag(&itn, &stplhs, &stprhs, &anorm, &sigmax,
work, lwork, &fail1);
printf("Converged in %12" NAG_IFMT " iterations\n", itn);
printf("Matrix norm = %11.3e\n", anorm);
printf("Final residual norm = %11.3e\n\n", stplhs);
/* Output x */
printf("%14s\n", "Solution");
for (i = 0; i < n; i++)
printf(" ( %13.4e, %13.4e) \n", x[i].re, x[i].im);
END:
NAG_FREE(a);
NAG_FREE(b);
NAG_FREE(rdiag);
NAG_FREE(work);
NAG_FREE(x);
NAG_FREE(wgt);
NAG_FREE(icol);
NAG_FREE(irow);
NAG_FREE(iwork);
return exit_status;
}