23 #include <grass/gis.h>
24 #include <grass/gmath.h>
25 #include <grass/glocale.h>
27 #define G_MATH_ROWSCALE_L2_NORM_PRECONDITION 1
28 #define G_MATH_ROWSCALE_L1_NORM_PRECONDITION 2
29 #define G_MATH_DIAGONAL_PRECONDITION 3
31 static G_math_spvector **create_diag_precond_matrix(
double **A,
32 G_math_spvector ** Asp,
34 static int solver_pcg(
double **A, G_math_spvector ** Asp,
double *x,
35 double *
b,
int rows,
int maxit,
double err,
int prec);
36 static int solver_cg(
double **A, G_math_spvector ** Asp,
double *x,
double *
b,
37 int rows,
int maxit,
double err);
38 static int solver_bicgstab(
double **A, G_math_spvector ** Asp,
double *x,
39 double *
b,
int rows,
int maxit,
double err);
68 return solver_pcg(A,
NULL, x, b, rows, maxit, err, prec);
94 int rows,
int maxit,
double err,
int prec)
97 return solver_pcg(
NULL, Asp, x, b, rows, maxit, err, prec);
100 int solver_pcg(
double **A, G_math_spvector ** Asp,
double *x,
double *
b,
101 int rows,
int maxit,
double err,
int prec)
111 double a0 = 0, a1 = 0, mygamma, tmp = 0;
129 M = create_diag_precond_matrix(A, Asp, rows, prec);
146 #pragma omp for schedule (static) private(i) reduction(+:s)
147 for (i = 0; i < rows; i++) {
158 for (m = 0; m < maxit; m++) {
159 #pragma omp parallel default(shared)
169 #pragma omp for schedule (static) private(i) reduction(+:s)
170 for (i = 0; i < rows; i++) {
201 #pragma omp for schedule (static) private(i) reduction(+:s)
202 for (i = 0; i < rows; i++) {
214 if (a1 < 0 || a1 == 0 || a1 > 0) {
219 (
"Unable to solve the linear equation system"));
227 G_message(_(
"Sparse PCG -- iteration %i error %g\n"), m, a0);
229 G_message(_(
"PCG -- iteration %i error %g\n"), m, a0);
231 if (error_break == 1) {
277 return solver_cg(A,
NULL, x, b, rows, maxit, err);
302 int rows,
int maxit,
double err)
304 return solver_cg(
NULL, Asp, x, b, rows, maxit, err);
308 int solver_cg(
double **A, G_math_spvector ** Asp,
double *x,
double *b,
309 int rows,
int maxit,
double err)
319 double a0 = 0, a1 = 0, mygamma, tmp = 0;
346 #pragma omp for schedule (static) private(i) reduction(+:s)
347 for (i = 0; i < rows; i++) {
358 for (m = 0; m < maxit; m++) {
359 #pragma omp parallel default(shared)
367 #pragma omp for schedule (static) private(i) reduction(+:s)
368 for (i = 0; i < rows; i++) {
395 #pragma omp for schedule (static) private(i) reduction(+:s)
396 for (i = 0; i < rows; i++) {
408 if (a1 < 0 || a1 == 0 || a1 > 0) {
413 (
"Unable to solve the linear equation system"));
421 G_message(_(
"Sparse CG -- iteration %i error %g\n"), m, a0);
423 G_message(_(
"CG -- iteration %i error %g\n"), m, a0);
425 if (error_break == 1) {
467 int maxit,
double err)
469 return solver_bicgstab(A,
NULL, x, b, rows, maxit, err);
494 double *b,
int rows,
int maxit,
double err)
496 return solver_bicgstab(
NULL, Asp, x, b, rows, maxit, err);
500 int solver_bicgstab(
double **A, G_math_spvector ** Asp,
double *x,
double *b,
501 int rows,
int maxit,
double err)
515 double s1 = 0.0, s2 = 0.0, s3 = 0.0;
517 double alpha = 0, beta = 0, omega, rr0 = 0,
error;
551 for (m = 0; m < maxit; m++) {
553 #pragma omp parallel default(shared)
561 #pragma omp for schedule (static) private(i) reduction(+:s1, s2, s3)
562 for (i = 0; i < rows; i++) {
572 if (error < 0 || error == 0 || error > 0) {
577 (
"Unable to solve the linear equation system"));
593 #pragma omp for schedule (static) private(i) reduction(+:s1, s2)
594 for (i = 0; i < rows; i++) {
609 #pragma omp for schedule (static) private(i) reduction(+:s1)
610 for (i = 0; i < rows; i++) {
616 beta = alpha / omega * s1 / rr0;
626 G_message(_(
"Sparse BiCGStab -- iteration %i error %g\n"), m,
629 G_message(_(
"BiCGStab -- iteration %i error %g\n"), m, error);
631 if (error_break == 1) {
662 G_math_spvector **create_diag_precond_matrix(
double **A,
663 G_math_spvector ** Asp,
int rows,
666 G_math_spvector **Msp;
668 int i, j,
cols = rows;
675 #pragma omp parallel for schedule (static) private(i, j, sum) shared(A, Msp, rows, cols, prec)
676 for (i = 0; i < rows; i++) {
682 for (j = 0; j <
cols; j++)
683 sum += A[i][j] * A[i][j];
684 spvect->values[0] = 1.0 / sqrt(sum);
688 for (j = 0; j <
cols; j++)
689 sum += fabs(A[i][j]);
690 spvect->values[0] = 1.0 / (sum);
694 spvect->values[0] = 1.0 / A[i][i];
699 spvect->index[0] = i;
706 #pragma omp parallel for schedule (static) private(i, j, sum) shared(Asp, Msp, rows, cols, prec)
707 for (i = 0; i < rows; i++) {
713 for (j = 0; j < Asp[i]->cols; j++)
714 sum += Asp[i]->values[j] * Asp[i]->values[j];
715 spvect->values[0] = 1.0 / sqrt(sum);
719 for (j = 0; j < Asp[i]->cols; j++)
720 sum += fabs(Asp[i]->values[j]);
721 spvect->values[0] = 1.0 / (sum);
725 for (j = 0; j < Asp[i]->cols; j++)
726 if (i == Asp[i]->index[j])
727 spvect->values[0] = 1.0 / Asp[i]->values[j];
731 spvect->index[0] = i;
void G_free(void *buf)
Free allocated memory.
#define G_MATH_DIAGONAL_PRECONDITION
G_math_spvector * G_math_alloc_spvector(int cols)
Allocate memory for a sparse vector.
void G_math_d_Ax(double **A, double *x, double *y, int rows, int cols)
Compute the matrix - vector product of matrix A and vector x.
int G_math_solver_sparse_pcg(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err, int prec)
The iterative preconditioned conjugate gradients solver for sparse symmetric positive definite matric...
def error
Display an error message using g.message -e
int G_math_solver_sparse_bicgstab(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err)
The iterative biconjugate gradients solver with stabilization for unsymmetric non-definite matrices...
G_math_spvector ** G_math_alloc_spmatrix(int rows)
Allocate memory for a sparse matrix.
void G_message(const char *msg,...)
Print a message to stderr.
#define G_MATH_ROWSCALE_L2_NORM_PRECONDITION
void G_math_d_copy(double *x, double *y, int rows)
Copy the vector x to y.
void G_math_free_spmatrix(G_math_spvector **spmatrix, int rows)
Release the memory of the sparse matrix.
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
void G_math_Ax_sparse(G_math_spvector **Asp, double *x, double *y, int rows)
Compute the matrix - vector product of sparse matrix **Asp and vector x.
#define G_MATH_ROWSCALE_L1_NORM_PRECONDITION
int G_math_solver_pcg(double **A, double *x, double *b, int rows, int maxit, double err, int prec)
The iterative preconditioned conjugate gradients solver for symmetric positive definite matrices...
void G_math_d_ax_by(double *x, double *y, double *z, double a, double b, int rows)
Scales vectors x and y with the scalars a and b and adds them.
double * G_alloc_vector(size_t n)
Vector matrix memory allocation.
int G_math_add_spvector(G_math_spvector **Asp, G_math_spvector *spvector, int row)
Adds a sparse vector to a sparse linear equation system at position row.
int G_math_solver_bicgstab(double **A, double *x, double *b, int rows, int maxit, double err)
The iterative biconjugate gradients solver with stabilization for unsymmetric non-definite matrices...
int G_math_solver_cg(double **A, double *x, double *b, int rows, int maxit, double err)
The iterative conjugate gradients solver for symmetric positive definite matrices.
int G_math_solver_sparse_cg(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err)
The iterative conjugate gradients solver for sparse symmetric positive definite matrices.