programmer's documentation
cs_sla.h
Go to the documentation of this file.
1 #ifndef __CS_SLA_H__
2 #define __CS_SLA_H__
3 
4 /*============================================================================
5  * Sparse linear algebra routines
6  *============================================================================*/
7 
8 /*
9  This file is part of Code_Saturne, a general-purpose CFD tool.
10 
11  Copyright (C) 1998-2018 EDF S.A.
12 
13  This program is free software; you can redistribute it and/or modify it under
14  the terms of the GNU General Public License as published by the Free Software
15  Foundation; either version 2 of the License, or (at your option) any later
16  version.
17 
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  details.
22 
23  You should have received a copy of the GNU General Public License along with
24  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25  Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27 
28 /*----------------------------------------------------------------------------*/
29 
30 /*----------------------------------------------------------------------------
31  * Standard C library headers
32  *----------------------------------------------------------------------------*/
33 
34 #include <stdio.h>
35 
36 /*----------------------------------------------------------------------------
37  * Local headers
38  *----------------------------------------------------------------------------*/
39 
40 #include "cs_base.h"
41 #include "cs_cdo.h"
42 #include "cs_sdm.h"
43 #include "cs_param.h"
44 #include "cs_mesh_adjacencies.h"
45 
46 /*----------------------------------------------------------------------------*/
47 
49 
50 /*============================================================================
51  * Macro definitions
52  *============================================================================*/
53 
54 /* Matrix flag */
55 #define CS_SLA_MATRIX_SYM (1 << 0) /* 1: symmetric */
56 #define CS_SLA_MATRIX_SORTED (1 << 1) /* 2: sorted */
57 #define CS_SLA_MATRIX_SHARED (1 << 2) /* 4: share pattern */
58 
59 /*============================================================================
60  * Type definitions
61  *============================================================================*/
62 
63 typedef enum {
64 
70 
72 
73 typedef struct {
74 
75  cs_sla_matrix_type_t type; /* Type of matrix storage */
76  int flag; /* Symmetric, sorted, shared... */
77 
78  int stride; /* Number of entries in "val" for each couple A(i,j) */
79  int n_rows;
80  int n_cols;
81 
84 
85  short int *sgn; /* Only for DEC type: -1 or 1 according to orientation */
86  double *val; /* Only for CSR and MSR type */
87 
88  cs_lnum_t *didx; /* Diagonal index: used to flag diagonal index and to
89  separate lower and upper part */
90  double *diag; /* Used in MSR type but can also be used for a
91  given preconditioning technique */
92 
94 
95 /* Specific matrix for hybrid discretization X+C (C for cells) and X=V or F
96 
97  This matrix corresponds to 4 blocks :
98  (0,0) --> square MSR matrix of size #X
99  (1,0) --> rectangular matrix based on a cs_connect_index_t (c2x)
100  (1,1) --> diagonal matrix of size number of cells
101  (0,1) --> rectangular matrix sharing the same pattern as block (1,0)
102  Transposed of (1,0) is the matrix is symmetric
103 
104  The resulting matrix is square matrix of size #C + #X */
105 
106 typedef struct {
107 
108  int flag; // Symmetric, sorted, shared...
109 
110  cs_lnum_t n_x; // n_vertices or n_faces
112  cs_lnum_t n_rows; // n_x + n_cells
113 
114  const cs_adjacency_t *c2x; /* shared with a cs_cdo_connect_t structure
115  which is owner. Enable an easy acces to
116  blocks (1,0) and (0,1) */
117 
118  cs_sla_matrix_t *xx_block; // (0,0) block = MSR-type matrix
119  double *cc_diag; // (1,1) block = diagonal matrix of size n_cells
120  double *cx_vals; // (1,0) block = rectangular based on c2x index
121  double *xc_vals; /* (0,1) block = rectangular based on c2x index
122  Only allocated if matrix is not symmetric */
123 
125 
126 /*============================================================================
127  * Public function prototypes for SLA matrices
128  *============================================================================*/
129 
130 /*----------------------------------------------------------------------------*/
142 /*----------------------------------------------------------------------------*/
143 
146  cs_lnum_t n_cols,
147  int stride,
149  bool sym);
150 
151 /*----------------------------------------------------------------------------*/
163 /*----------------------------------------------------------------------------*/
164 
168  int stride);
169 
170 /*----------------------------------------------------------------------------*/
184 /*----------------------------------------------------------------------------*/
185 
188  bool is_symmetric,
189  bool sorted_idx,
190  int stride);
191 
192 /*----------------------------------------------------------------------------*/
201 /*----------------------------------------------------------------------------*/
202 
205  bool shared);
206 
207 /*----------------------------------------------------------------------------*/
215 /*----------------------------------------------------------------------------*/
216 
219 
220 /*----------------------------------------------------------------------------*/
228 /*----------------------------------------------------------------------------*/
229 
232 
233 /*----------------------------------------------------------------------------*/
242 /*----------------------------------------------------------------------------*/
243 
244 void
246  double threshold,
247  int verbosity);
248 
249 /*----------------------------------------------------------------------------*/
260 /*----------------------------------------------------------------------------*/
261 
262 void
263 cs_sla_matrix_clean(int verbosity,
264  double threshold,
265  cs_sla_matrix_t *m);
266 
267 /*----------------------------------------------------------------------------*/
275 /*----------------------------------------------------------------------------*/
276 
277 size_t
279 
280 /*----------------------------------------------------------------------------*/
286 /*----------------------------------------------------------------------------*/
287 
288 void
290 
291 /*----------------------------------------------------------------------------*/
298 /*----------------------------------------------------------------------------*/
299 
300 void
302  double *p_diag[]);
303 
304 /*----------------------------------------------------------------------------*/
310 /*----------------------------------------------------------------------------*/
311 
312 void
314 
315 /*----------------------------------------------------------------------------*/
321 /*----------------------------------------------------------------------------*/
322 
323 void
325 
326 /*----------------------------------------------------------------------------*/
332 /*----------------------------------------------------------------------------*/
333 
334 void
336 
337 /*----------------------------------------------------------------------------*/
343 /*----------------------------------------------------------------------------*/
344 
345 void
347 
348 /*----------------------------------------------------------------------------*/
363 /*----------------------------------------------------------------------------*/
364 
366 cs_sla_matrix_pack(cs_lnum_t n_final_rows,
367  cs_lnum_t n_final_cols,
368  const cs_sla_matrix_t *init,
369  const cs_lnum_t *row_z2i_ids,
370  const cs_lnum_t *col_i2z_ids,
371  bool keep_sym);
372 
373 /*----------------------------------------------------------------------------*/
384 /*----------------------------------------------------------------------------*/
385 
387 cs_sla_matrix_add(double alpha,
388  const cs_sla_matrix_t *a,
389  double beta,
390  const cs_sla_matrix_t *b);
391 
392 /*----------------------------------------------------------------------------*/
403 /*----------------------------------------------------------------------------*/
404 
405 void
407  const double v[],
408  double *inout[],
409  bool reset);
410 
411 /*----------------------------------------------------------------------------*/
423 /*----------------------------------------------------------------------------*/
424 
425 void
426 cs_sla_amxby(double alpha,
427  const cs_sla_matrix_t *m,
428  const double x[],
429  double beta,
430  const double y[],
431  double *inout[]);
432 
433 /*----------------------------------------------------------------------------*/
442 /*----------------------------------------------------------------------------*/
443 
446  const cs_sla_matrix_t *b);
447 
448 /*----------------------------------------------------------------------------*/
459 /*----------------------------------------------------------------------------*/
460 
463  const double D[],
464  const cs_sla_matrix_t *A,
465  cs_lnum_t *w);
466 
467 /*----------------------------------------------------------------------------*/
480 /*----------------------------------------------------------------------------*/
481 
484  const cs_sla_matrix_t *a,
485  double beta,
486  const cs_sla_matrix_t *bt,
487  const cs_sla_matrix_t *b);
488 
489 /*----------------------------------------------------------------------------*/
506 /*----------------------------------------------------------------------------*/
507 
508 void
510  const cs_sla_matrix_t *B,
511  const cs_sla_matrix_t *C,
512  const cs_sla_matrix_t *D,
513  const double X[],
514  const double Y[],
515  double *F[],
516  double *G[],
517  bool reset);
518 
519 /*----------------------------------------------------------------------------*/
529 /*----------------------------------------------------------------------------*/
530 
531 void
532 cs_sla_bwrite(const char *name,
533  const cs_sla_matrix_t *m,
534  const double *rhs,
535  const double *sol);
536 
537 /*----------------------------------------------------------------------------*/
547 /*----------------------------------------------------------------------------*/
548 
549 void
550 cs_sla_bread(const char *name,
551  cs_sla_matrix_t **p_mat,
552  double *p_rhs[],
553  double *p_sol[]);
554 
555 /*----------------------------------------------------------------------------*/
563 /*----------------------------------------------------------------------------*/
564 
565 void
566 cs_sla_matrix_summary(const char *name,
567  FILE *f,
568  cs_sla_matrix_t *m);
569 
570 /*----------------------------------------------------------------------------*/
578 /*----------------------------------------------------------------------------*/
579 
580 void
581 cs_sla_matrix_dump(const char *name,
582  FILE *f,
583  const cs_sla_matrix_t *m);
584 
585 /*----------------------------------------------------------------------------*/
594 /*----------------------------------------------------------------------------*/
595 
596 void
597 cs_sla_system_dump(const char *name,
598  FILE *f,
599  const cs_sla_matrix_t *m,
600  const double *rhs);
601 
602 /*----------------------------------------------------------------------------*/
616 /*----------------------------------------------------------------------------*/
617 
620  cs_lnum_t n_cells,
621  bool bktrans,
622  bool bk00sym,
623  const cs_adjacency_t *x2x,
624  const cs_adjacency_t *c2x);
625 
626 /*----------------------------------------------------------------------------*/
634 /*----------------------------------------------------------------------------*/
635 
638 
639 /*----------------------------------------------------------------------------*/
652 /*----------------------------------------------------------------------------*/
653 
654 void
656  const double vx[],
657  const double vc[],
658  double *iox[],
659  double *ioc[],
660  bool reset);
661 
662 /*----------------------------------------------------------------------------*/
663 
665 
666 #endif /* __CS_SLA_H__ */
double * cc_diag
Definition: cs_sla.h:119
cs_sla_matrix_t * cs_sla_matrix_create_msr_from_index(const cs_adjacency_t *connect_idx, bool is_symmetric, bool sorted_idx, int stride)
Create a cs_sla_matrix_t structure with MSR type from an existing connectivity index. Be aware of removing the diagonal entry in the connectivity index before the call to this routine.
Definition: cs_sla.c:1409
void cs_sla_system_dump(const char *name, FILE *f, const cs_sla_matrix_t *m, const double *rhs)
Dump a cs_sla_matrix_t structure and its related right-hand side.
Definition: cs_sla.c:3264
cs_sla_matrix_t * cs_sla_matrix_copy(const cs_sla_matrix_t *a, bool shared)
Create a new matrix structure from the copy of an existing one.
Definition: cs_sla.c:1465
Definition: cs_sla.h:65
void cs_sla_hmatvec(const cs_sla_hmatrix_t *hm, const double vx[], const double vc[], double *iox[], double *ioc[], bool reset)
Compute a matrix vector product. If inout is not allocated, allocation is done inside this function I...
Definition: cs_sla.c:3474
void cs_sla_matrix_summary(const char *name, FILE *f, cs_sla_matrix_t *m)
Synthesis of a cs_sla_matrix_t structure.
Definition: cs_sla.c:3105
cs_sla_matrix_t * cs_sla_multiply_AtDA(const cs_sla_matrix_t *At, const double D[], const cs_sla_matrix_t *A, cs_lnum_t *w)
Compute the product C = At * Diag * A.
Definition: cs_sla.c:2655
double * diag
Definition: cs_sla.h:90
void cs_sla_bwrite(const char *name, const cs_sla_matrix_t *m, const double *rhs, const double *sol)
Write in binary format a matrix in CSR format, its righ hand side and the solution.
Definition: cs_sla.c:3025
double * val
Definition: cs_sla.h:86
cs_sla_matrix_t * cs_sla_matrix_combine(double alpha, const cs_sla_matrix_t *a, double beta, const cs_sla_matrix_t *bt, const cs_sla_matrix_t *b)
Specific matrix multiplication. Compute Bt * beta * B + alpha * A where alpha and beta are scalar...
Definition: cs_sla.c:2718
void cs_sla_matvec(const cs_sla_matrix_t *m, const double v[], double *inout[], bool reset)
Compute a matrix vector product. If inout is not allocated, allocation is done inside this function I...
Definition: cs_sla.c:2460
Definition: cs_sla.h:106
cs_sla_matrix_t * cs_sla_matrix_multiply(const cs_sla_matrix_t *a, const cs_sla_matrix_t *b)
Main subroutine to multiply two sparse matrices a and b. c= a*b.
Definition: cs_sla.c:2576
Definition: cs_mesh_adjacencies.h:89
void cs_sla_matrix_clean_zeros(cs_sla_matrix_t *m, double threshold, int verbosity)
Reset to 0 all entries below a given threshold Only available for CSR and MSR matrices with stride = ...
Definition: cs_sla.c:1701
cs_sla_matrix_t * cs_sla_matrix_add(double alpha, const cs_sla_matrix_t *a, double beta, const cs_sla_matrix_t *b)
Add two sparse matrices a and b. c = alpha*a + beta*b.
Definition: cs_sla.c:2314
const cs_adjacency_t * c2x
Definition: cs_sla.h:114
double * xc_vals
Definition: cs_sla.h:121
#define BEGIN_C_DECLS
Definition: cs_defs.h:453
cs_sla_matrix_t * cs_sla_matrix_transpose(const cs_sla_matrix_t *mat)
Transpose a cs_sla_matrix_t structure.
Definition: cs_sla.c:1541
int stride
Definition: cs_sla.h:78
Definition: cs_field_pointer.h:83
void cs_sla_matrix_clean(int verbosity, double threshold, cs_sla_matrix_t *m)
Set to zero entries in a cs_sla_matrix_t structure if the ratio |a(i,j)| < eps * max|a(i,j)| is below a given threshold. Be careful when using this function since one can loose the symmetry Only available for matrices with stride = 1.
Definition: cs_sla.c:1747
cs_lnum_t n_x
Definition: cs_sla.h:110
double precision, dimension(ncharm), save beta
Definition: cpincl.f90:99
cs_lnum_t * idx
Definition: cs_sla.h:82
void cs_sla_matrix_dump(const char *name, FILE *f, const cs_sla_matrix_t *m)
Dump a cs_sla_matrix_t structure.
Definition: cs_sla.c:3171
void cs_sla_matrix_csr2msr(cs_sla_matrix_t *a)
Change matrix representation from CSR to MSR.
Definition: cs_sla.c:2070
cs_lnum_t n_cells
Definition: cs_sla.h:111
short int * sgn
Definition: cs_sla.h:85
Definition: cs_sla.h:73
double precision, dimension(:,:,:), allocatable v
Definition: atimbr.f90:114
void cs_sla_bread(const char *name, cs_sla_matrix_t **p_mat, double *p_rhs[], double *p_sol[])
Read from a binary file a matrix in CSR format, its righ hand side and the solution. Matrix must have a stride equal to 1.
Definition: cs_sla.c:2946
cs_sla_matrix_t * cs_sla_matrix_free(cs_sla_matrix_t *m)
Free a cs_sla_matrix_t structure.
Definition: cs_sla.c:1647
void cs_sla_amxby(double alpha, const cs_sla_matrix_t *m, const double x[], double beta, const double y[], double *inout[])
Compute an array resulting from alpha * M(x) + beta * y If inout is not allocated, allocation is done inside this function.
Definition: cs_sla.c:2542
cs_sla_matrix_t * cs_sla_matrix_pack(cs_lnum_t n_final_rows, cs_lnum_t n_final_cols, const cs_sla_matrix_t *init, const cs_lnum_t *row_z2i_ids, const cs_lnum_t *col_i2z_ids, bool keep_sym)
Build a new matrix resulting from the extraction of some listed rows and columns. The output is a new...
double precision, save a
Definition: cs_fuel_incl.f90:146
cs_sla_matrix_t * cs_sla_matrix_create_from_ref(const cs_sla_matrix_t *ref, cs_sla_matrix_type_t type, int stride)
Create a cs_sla_matrix_t structure from an existing one. idx, didx and col_id are shared with ref...
Definition: cs_sla.c:1324
int n_cols
Definition: cs_sla.h:80
int n_rows
Definition: cs_sla.h:79
Definition: cs_sla.h:67
void cs_sla_matrix_msr2csr(cs_sla_matrix_t *a)
Change matrix representation from MSR to CSR.
Definition: cs_sla.c:1999
cs_sla_matrix_type_t type
Definition: cs_sla.h:75
Definition: cs_sla.h:69
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:293
void cs_sla_matrix_sort(cs_sla_matrix_t *m)
Sort each row by increasing colomn number.
Definition: cs_sla.c:1964
int flag
Definition: cs_sla.h:108
cs_lnum_t n_rows
Definition: cs_sla.h:112
#define END_C_DECLS
Definition: cs_defs.h:454
cs_sla_matrix_type_t
Definition: cs_sla.h:63
void cs_sla_matrix_share2own(cs_sla_matrix_t *a)
Allocate its own pattern if shared.
Definition: cs_sla.c:2134
void cs_sla_matvec_block2(const cs_sla_matrix_t *A, const cs_sla_matrix_t *B, const cs_sla_matrix_t *C, const cs_sla_matrix_t *D, const double X[], const double Y[], double *F[], double *G[], bool reset)
Matrix block 2x2 multiply by a vector.
Definition: cs_sla.h:66
cs_sla_hmatrix_t * cs_sla_hmatrix_create(cs_lnum_t n_x, cs_lnum_t n_cells, bool bktrans, bool bk00sym, const cs_adjacency_t *x2x, const cs_adjacency_t *c2x)
Create a cs_sla_hmatrix_t structure This is a square matrix of size n_x+n_cells (stride = 1 up to now...
Definition: cs_sla.c:3371
int flag
Definition: cs_sla.h:76
void cs_sla_matrix_get_diag(const cs_sla_matrix_t *m, double *p_diag[])
Get the diagonal entries of a given matrix.
Definition: cs_sla.c:1867
cs_sla_matrix_t * xx_block
Definition: cs_sla.h:118
cs_sla_hmatrix_t * cs_sla_hmatrix_free(cs_sla_hmatrix_t *hm)
Free a cs_sla_hmatrix_t structure.
Definition: cs_sla.c:3438
Definition: cs_sla.h:68
size_t cs_sla_matrix_get_nnz(const cs_sla_matrix_t *m)
Retrieve the number of non-zeros (nnz) elements in a matrix.
Definition: cs_sla.c:1800
cs_lnum_t * didx
Definition: cs_sla.h:88
cs_lnum_t * col_id
Definition: cs_sla.h:83
double * cx_vals
Definition: cs_sla.h:120
void cs_sla_matrix_diag_idx(cs_sla_matrix_t *m)
Build diagonal index.
Definition: cs_sla.c:1826
cs_sla_matrix_t * cs_sla_matrix_create(cs_lnum_t n_rows, cs_lnum_t n_cols, int stride, cs_sla_matrix_type_t type, bool sym)
Create a cs_sla_matrix_t structure.
Definition: cs_sla.c:1255
double precision, save b
Definition: cs_fuel_incl.f90:146