/* nag_sparse_nherm_precon_ssor_solve (f11drc) Example Program. * * Copyright 2011, Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include 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_nherm_precon_ssor_solve (f11drc) Example Program Results"); printf("\n\n"); /* Skip heading in data file*/ scanf("%*[^\n]"); /* Read algorithmic parameters*/ scanf("%ld%ld%*[^\n]", &n, &m); scanf("%ld%*[^\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("%s%*[^\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("%s%*[^\n]", nag_enum_arg); precon = (Nag_SparseNsym_PrecType) nag_enum_name_to_value(nag_enum_arg); scanf("%s%*[^\n]", nag_enum_arg); norm = (Nag_NormType) nag_enum_name_to_value(nag_enum_arg); scanf("%ld%*[^\n]", &iterm); scanf("%lf%ld%*[^\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 ) %ld%ld%*[^\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_nherm_basic_setup (f11brc) * Complex sparse non-Hermitian linear systems, setup */ nag_sparse_nherm_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_nherm_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(0.0, 0.0))) { printf("Matrix has a zero diagonal element\n"); goto END; } else { rdiag[irow[i]-1] = nag_complex_divide(nag_complex(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_nherm_basic_solver (f11bsc). * Complex sparse non-Hermitian linear systems, solver routine * preconditioned RGMRES, CGS, Bi-CGSTAB or TFQMR method */ nag_sparse_nherm_basic_solver(&irevcm, x, b, wgt, work, lwork, &fail); switch (irevcm) { case 1: /* Compute matrix-vector product*/ trans = Nag_NoTrans; /* nag_sparse_nherm_matvec (f11xnc). * Complex sparse non-Hermitian matrix vector multiply */ nag_sparse_nherm_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_nherm_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_nherm_precon_ssor_solve (f11drc). * Solution of linear system involving preconditioning matrix generated * by applying SSOR to complex sparse non-Hermitian matrix */ nag_sparse_nherm_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_nherm_basic_solver (f11bsc).\n%s\n", fail.message); exit_status = 3; goto END; } /* nag_sparse_nherm_basic_diagnostic (f11btc) * Complex sparse non-Hermitian linear systems, diagnostic */ nag_sparse_nherm_basic_diagnostic(&itn, &stplhs, &stprhs, &anorm, &sigmax, work, lwork, &fail1); printf("Converged in %12ld 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(" ( %11.4e, %11.4e) \n", x[i].re, x[i].im); END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (rdiag) NAG_FREE(rdiag); if (work) NAG_FREE(work); if (x) NAG_FREE(x); if (wgt) NAG_FREE(wgt); if (icol) NAG_FREE(icol); if (irow) NAG_FREE(irow); if (iwork) NAG_FREE(iwork); return exit_status; }