27#include "fasp_functs.h"
75 INT iter = 0, stag = 1, more_step = 1, restart_step = 1;
78 REAL reldiff, factor, normuinf;
79 REAL alpha, beta, temp1, temp2;
85 REAL *p = work, *z = work+m, *r = z+m, *t = r+m, *u_best = t+m;
88 if ( PrtLvl >
PRINT_NONE ) printf(
"\nCalling Safe CG solver (CSR) ...\n");
91 printf(
"### DEBUG: [-Begin-] %s ...\n", __FUNCTION__);
92 printf(
"### DEBUG: maxit = %d, tol = %.4le\n", MaxIt, tol);
109 relres = absres0/normr0;
114 relres = absres0/normr0;
119 relres = absres0/normu;
122 printf(
"### ERROR: Unknown stopping type! [%s]\n", __FUNCTION__);
127 if ( relres < tol )
goto FINISHED;
130 fasp_itinfo(PrtLvl,StopType,iter,relres,absres0,0.0);
136 while ( iter++ < MaxIt ) {
147 goto RESTORE_BESTSOL;
157 switch ( StopType ) {
160 relres = absres/normr0;
169 relres = absres/normr0;
173 relres = absres/normu;
178 factor = absres/absres0;
181 fasp_itinfo(PrtLvl,StopType,iter,relres,absres,factor);
186 goto RESTORE_BESTSOL;
190 if ( absres < absres_best - maxdiff) {
191 absres_best = absres;
198 if ( normuinf <= sol_inf_tol ) {
209 if ( (stag <= MaxStag) & (reldiff < maxdiff) ) {
212 ITS_DIFFRES(reldiff,relres);
220 switch ( StopType ) {
223 relres = absres/normr0;
232 relres = absres/normr0;
236 relres = absres/normu;
240 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
245 if ( stag >= MaxStag ) {
257 if ( relres < tol ) {
259 if ( PrtLvl >=
PRINT_MORE ) ITS_COMPRES(relres);
266 switch ( StopType ) {
269 relres = absres/normr0;
278 relres = absres/normr0;
282 relres = absres/normu;
286 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
289 if ( relres < tol )
break;
291 if ( more_step >= MaxRestartStep ) {
326 if ( iter != iter_best ) {
332 switch ( StopType ) {
349 if ( absres > absres_best + maxdiff || isnan(absres) ) {
350 if ( PrtLvl >
PRINT_NONE ) ITS_RESTORE(iter_best);
352 relres = absres_best / normr0;
357 if ( PrtLvl >
PRINT_NONE ) ITS_FINAL(iter,MaxIt,relres);
363 printf(
"### DEBUG: [--End--] %s ...\n", __FUNCTION__);
399 const SHORT StopType,
408 INT iter = 0, stag = 1, more_step = 1, restart_step = 1;
411 REAL reldiff, factor, normuinf;
412 REAL alpha, beta, temp1, temp2;
418 REAL *p = work, *z = work+m, *r = z+m, *t = r+m, *u_best = t+m;
421 if ( PrtLvl >
PRINT_NONE ) printf(
"\nCalling Safe CG solver (BLC) ...\n");
424 printf(
"### DEBUG: [-Begin-] %s ...\n", __FUNCTION__);
425 printf(
"### DEBUG: maxit = %d, tol = %.4le\n", MaxIt, tol);
442 relres = absres0/normr0;
447 relres = absres0/normr0;
452 relres = absres0/normu;
455 printf(
"### ERROR: Unknown stopping type! [%s]\n", __FUNCTION__);
460 if ( relres < tol )
goto FINISHED;
463 fasp_itinfo(PrtLvl,StopType,iter,relres,absres0,0.0);
469 while ( iter++ < MaxIt ) {
480 goto RESTORE_BESTSOL;
490 switch ( StopType ) {
493 relres = absres/normr0;
502 relres = absres/normr0;
506 relres = absres/normu;
511 factor = absres/absres0;
514 fasp_itinfo(PrtLvl,StopType,iter,relres,absres,factor);
519 goto RESTORE_BESTSOL;
523 if ( absres < absres_best - maxdiff) {
524 absres_best = absres;
531 if ( normuinf <= sol_inf_tol ) {
542 if ( (stag <= MaxStag) & (reldiff < maxdiff) ) {
545 ITS_DIFFRES(reldiff,relres);
553 switch ( StopType ) {
556 relres = absres/normr0;
565 relres = absres/normr0;
569 relres = absres/normu;
573 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
578 if ( stag >= MaxStag ) {
590 if ( relres < tol ) {
592 if ( PrtLvl >=
PRINT_MORE ) ITS_COMPRES(relres);
599 switch ( StopType ) {
602 relres = absres/normr0;
611 relres = absres/normr0;
615 relres = absres/normu;
619 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
622 if ( relres < tol )
break;
624 if ( more_step >= MaxRestartStep ) {
659 if ( iter != iter_best ) {
665 switch ( StopType ) {
682 if ( absres > absres_best + maxdiff || isnan(absres) ) {
683 if ( PrtLvl >
PRINT_NONE ) ITS_RESTORE(iter_best);
685 relres = absres_best / normr0;
690 if ( PrtLvl >
PRINT_NONE ) ITS_FINAL(iter,MaxIt,relres);
696 printf(
"### DEBUG: [--End--] %s ...\n", __FUNCTION__);
732 const SHORT StopType,
741 INT iter = 0, stag = 1, more_step = 1, restart_step = 1;
744 REAL reldiff, factor, normuinf;
745 REAL alpha, beta, temp1, temp2;
751 REAL *p = work, *z = work+m, *r = z+m, *t = r+m, *u_best = t+m;
754 if ( PrtLvl >
PRINT_NONE ) printf(
"\nCalling Safe CG solver (STR) ...\n");
757 printf(
"### DEBUG: [-Begin-] %s ...\n", __FUNCTION__);
758 printf(
"### DEBUG: maxit = %d, tol = %.4le\n", MaxIt, tol);
775 relres = absres0/normr0;
780 relres = absres0/normr0;
785 relres = absres0/normu;
788 printf(
"### ERROR: Unknown stopping type! [%s]\n", __FUNCTION__);
793 if ( relres < tol )
goto FINISHED;
796 fasp_itinfo(PrtLvl,StopType,iter,relres,absres0,0.0);
802 while ( iter++ < MaxIt ) {
813 goto RESTORE_BESTSOL;
823 switch ( StopType ) {
826 relres = absres/normr0;
835 relres = absres/normr0;
839 relres = absres/normu;
844 factor = absres/absres0;
847 fasp_itinfo(PrtLvl,StopType,iter,relres,absres,factor);
852 goto RESTORE_BESTSOL;
856 if ( absres < absres_best - maxdiff) {
857 absres_best = absres;
864 if ( normuinf <= sol_inf_tol ) {
875 if ( (stag <= MaxStag) & (reldiff < maxdiff) ) {
878 ITS_DIFFRES(reldiff,relres);
886 switch ( StopType ) {
889 relres = absres/normr0;
898 relres = absres/normr0;
902 relres = absres/normu;
906 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
911 if ( stag >= MaxStag ) {
923 if ( relres < tol ) {
925 if ( PrtLvl >=
PRINT_MORE ) ITS_COMPRES(relres);
932 switch ( StopType ) {
935 relres = absres/normr0;
944 relres = absres/normr0;
948 relres = absres/normu;
952 if ( PrtLvl >=
PRINT_MORE ) ITS_REALRES(relres);
955 if ( relres < tol )
break;
957 if ( more_step >= MaxRestartStep ) {
992 if ( iter != iter_best ) {
998 switch ( StopType ) {
1015 if ( absres > absres_best + maxdiff || isnan(absres) ) {
1016 if ( PrtLvl >
PRINT_NONE ) ITS_RESTORE(iter_best);
1018 relres = absres_best / normr0;
1023 if ( PrtLvl >
PRINT_NONE ) ITS_FINAL(iter,MaxIt,relres);
1029 printf(
"### DEBUG: [--End--] %s ...\n", __FUNCTION__);
void fasp_darray_set(const INT n, REAL *x, const REAL val)
Set initial value for an array to be x=val.
void fasp_darray_cp(const INT n, const REAL *x, REAL *y)
Copy an array to the other y=x.
void fasp_mem_free(void *mem)
Free up previous allocated memory body and set pointer to NULL.
void * fasp_mem_calloc(const unsigned int size, const unsigned int type)
Allocate, initiate, and check memory.
void fasp_itinfo(const INT ptrlvl, const INT stop_type, const INT iter, const REAL relres, const REAL absres, const REAL factor)
Print out iteration information for iterative solvers.
SHORT fasp_dvec_isnan(const dvector *u)
Check a dvector whether there is NAN.
REAL fasp_blas_darray_dotprod(const INT n, const REAL *x, const REAL *y)
Inner product of two arraies x and y.
REAL fasp_blas_darray_norminf(const INT n, const REAL *x)
Linf norm of array x.
void fasp_blas_darray_axpby(const INT n, const REAL a, const REAL *x, const REAL b, REAL *y)
y = a*x + b*y
REAL fasp_blas_darray_norm2(const INT n, const REAL *x)
L2 norm of array x.
void fasp_blas_darray_axpy(const INT n, const REAL a, const REAL *x, REAL *y)
y = a*x + y
void fasp_blas_dblc_mxv(const dBLCmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = A*x.
void fasp_blas_dblc_aAxpy(const REAL alpha, const dBLCmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = alpha*A*x + y.
void fasp_blas_dcsr_mxv(const dCSRmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = A*x.
void fasp_blas_dcsr_aAxpy(const REAL alpha, const dCSRmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = alpha*A*x + y.
void fasp_blas_dstr_aAxpy(const REAL alpha, const dSTRmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = alpha*A*x + y.
void fasp_blas_dstr_mxv(const dSTRmat *A, const REAL *x, REAL *y)
Matrix-vector multiplication y = A*x.
REAL fasp_blas_dvec_norm2(const dvector *x)
L2 norm of dvector x.
INT fasp_solver_dstr_spcg(const dSTRmat *A, const dvector *b, dvector *u, precond *pc, const REAL tol, const INT MaxIt, const SHORT StopType, const SHORT PrtLvl)
Preconditioned conjugate gradient method for solving Au=b with safety net.
INT fasp_solver_dcsr_spcg(const dCSRmat *A, const dvector *b, dvector *u, precond *pc, const REAL tol, const INT MaxIt, const SHORT StopType, const SHORT PrtLvl)
Preconditioned conjugate gradient method for solving Au=b with safety net.
INT fasp_solver_dblc_spcg(const dBLCmat *A, const dvector *b, dvector *u, precond *pc, const REAL tol, const INT MaxIt, const SHORT StopType, const SHORT PrtLvl)
Preconditioned conjugate gradient method for solving Au=b with safety net.
Main header file for the FASP project.
#define SHORT
FASP integer and floating point numbers.
#define MAX(a, b)
Definition of max, min, abs.
#define ERROR_SOLVER_STAG
#define ERROR_SOLVER_MAXIT
#define STOP_REL_RES
Definition of iterative solver stopping criteria types.
#define ERROR_SOLVER_SOLSTAG
#define BIGREAL
Some global constants.
#define PRINT_NONE
Print level for all subroutines – not including DEBUG output.
#define ERROR_SOLVER_TOLSMALL
Block REAL CSR matrix format.
Sparse matrix of REAL type in CSR format.
Structure matrix of REAL type.
Vector with n entries of REAL type.
REAL * val
actual vector entries
Preconditioner data and action.
void * data
data for preconditioner, void pointer
void(* fct)(REAL *, REAL *, void *)
action for preconditioner, void function pointer