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_sdm.h"
42 #include "cs_param.h"
43 #include "cs_mesh_adjacencies.h"
44 
45 /*----------------------------------------------------------------------------*/
46 
48 
49 /*============================================================================
50  * Macro definitions
51  *============================================================================*/
52 
53 /* Matrix flag */
54 #define CS_SLA_MATRIX_SYM (1 << 0) /* 1: symmetric */
55 #define CS_SLA_MATRIX_SORTED (1 << 1) /* 2: sorted */
56 #define CS_SLA_MATRIX_SHARED (1 << 2) /* 4: share pattern */
57 
58 /*============================================================================
59  * Type definitions
60  *============================================================================*/
61 
62 typedef enum {
63 
69 
71 
72 typedef struct {
73 
74  cs_sla_matrix_type_t type; /* Type of matrix storage */
75  int flag; /* Symmetric, sorted, shared... */
76 
77  int stride; /* Number of entries in "val" for each couple A(i,j) */
78  int n_rows;
79  int n_cols;
80 
83 
84  short int *sgn; /* Only for DEC type: -1 or 1 according to orientation */
85  double *val; /* Only for CSR and MSR type */
86 
87  cs_lnum_t *didx; /* Diagonal index: used to flag diagonal index and to
88  separate lower and upper part */
89  double *diag; /* Used in MSR type but can also be used for a
90  given preconditioning technique */
91 
93 
94 /* Specific matrix for hybrid discretization X+C (C for cells) and X=V or F
95 
96  This matrix corresponds to 4 blocks :
97  (0,0) --> square MSR matrix of size #X
98  (1,0) --> rectangular matrix based on a cs_connect_index_t (c2x)
99  (1,1) --> diagonal matrix of size number of cells
100  (0,1) --> rectangular matrix sharing the same pattern as block (1,0)
101  Transposed of (1,0) is the matrix is symmetric
102 
103  The resulting matrix is square matrix of size #C + #X */
104 
105 typedef struct {
106 
107  int flag; // Symmetric, sorted, shared...
108 
109  cs_lnum_t n_x; // n_vertices or n_faces
111  cs_lnum_t n_rows; // n_x + n_cells
112 
113  const cs_adjacency_t *c2x; /* shared with a cs_cdo_connect_t structure
114  which is owner. Enable an easy acces to
115  blocks (1,0) and (0,1) */
116 
117  cs_sla_matrix_t *xx_block; // (0,0) block = MSR-type matrix
118  double *cc_diag; // (1,1) block = diagonal matrix of size n_cells
119  double *cx_vals; // (1,0) block = rectangular based on c2x index
120  double *xc_vals; /* (0,1) block = rectangular based on c2x index
121  Only allocated if matrix is not symmetric */
122 
124 
125 /*============================================================================
126  * Public function prototypes for SLA matrices
127  *============================================================================*/
128 
129 /*----------------------------------------------------------------------------*/
141 /*----------------------------------------------------------------------------*/
142 
145  cs_lnum_t n_cols,
146  int stride,
148  bool sym);
149 
150 /*----------------------------------------------------------------------------*/
162 /*----------------------------------------------------------------------------*/
163 
167  int stride);
168 
169 /*----------------------------------------------------------------------------*/
183 /*----------------------------------------------------------------------------*/
184 
187  bool is_symmetric,
188  bool sorted_idx,
189  int stride);
190 
191 /*----------------------------------------------------------------------------*/
200 /*----------------------------------------------------------------------------*/
201 
204  bool shared);
205 
206 /*----------------------------------------------------------------------------*/
214 /*----------------------------------------------------------------------------*/
215 
218 
219 /*----------------------------------------------------------------------------*/
227 /*----------------------------------------------------------------------------*/
228 
231 
232 /*----------------------------------------------------------------------------*/
241 /*----------------------------------------------------------------------------*/
242 
243 void
245  double threshold,
246  int verbosity);
247 
248 /*----------------------------------------------------------------------------*/
259 /*----------------------------------------------------------------------------*/
260 
261 void
262 cs_sla_matrix_clean(int verbosity,
263  double threshold,
264  cs_sla_matrix_t *m);
265 
266 /*----------------------------------------------------------------------------*/
274 /*----------------------------------------------------------------------------*/
275 
276 size_t
278 
279 /*----------------------------------------------------------------------------*/
285 /*----------------------------------------------------------------------------*/
286 
287 void
289 
290 /*----------------------------------------------------------------------------*/
297 /*----------------------------------------------------------------------------*/
298 
299 void
301  double *p_diag[]);
302 
303 /*----------------------------------------------------------------------------*/
309 /*----------------------------------------------------------------------------*/
310 
311 void
313 
314 /*----------------------------------------------------------------------------*/
320 /*----------------------------------------------------------------------------*/
321 
322 void
324 
325 /*----------------------------------------------------------------------------*/
331 /*----------------------------------------------------------------------------*/
332 
333 void
335 
336 /*----------------------------------------------------------------------------*/
342 /*----------------------------------------------------------------------------*/
343 
344 void
346 
347 /*----------------------------------------------------------------------------*/
362 /*----------------------------------------------------------------------------*/
363 
365 cs_sla_matrix_pack(cs_lnum_t n_final_rows,
366  cs_lnum_t n_final_cols,
367  const cs_sla_matrix_t *init,
368  const cs_lnum_t *row_z2i_ids,
369  const cs_lnum_t *col_i2z_ids,
370  bool keep_sym);
371 
372 /*----------------------------------------------------------------------------*/
383 /*----------------------------------------------------------------------------*/
384 
386 cs_sla_matrix_add(double alpha,
387  const cs_sla_matrix_t *a,
388  double beta,
389  const cs_sla_matrix_t *b);
390 
391 /*----------------------------------------------------------------------------*/
402 /*----------------------------------------------------------------------------*/
403 
404 void
406  const double v[],
407  double *inout[],
408  bool reset);
409 
410 /*----------------------------------------------------------------------------*/
422 /*----------------------------------------------------------------------------*/
423 
424 void
425 cs_sla_amxby(double alpha,
426  const cs_sla_matrix_t *m,
427  const double x[],
428  double beta,
429  const double y[],
430  double *inout[]);
431 
432 /*----------------------------------------------------------------------------*/
441 /*----------------------------------------------------------------------------*/
442 
445  const cs_sla_matrix_t *b);
446 
447 /*----------------------------------------------------------------------------*/
458 /*----------------------------------------------------------------------------*/
459 
462  const double D[],
463  const cs_sla_matrix_t *A,
464  cs_lnum_t *w);
465 
466 /*----------------------------------------------------------------------------*/
479 /*----------------------------------------------------------------------------*/
480 
483  const cs_sla_matrix_t *a,
484  double beta,
485  const cs_sla_matrix_t *bt,
486  const cs_sla_matrix_t *b);
487 
488 /*----------------------------------------------------------------------------*/
505 /*----------------------------------------------------------------------------*/
506 
507 void
509  const cs_sla_matrix_t *B,
510  const cs_sla_matrix_t *C,
511  const cs_sla_matrix_t *D,
512  const double X[],
513  const double Y[],
514  double *F[],
515  double *G[],
516  bool reset);
517 
518 /*----------------------------------------------------------------------------*/
528 /*----------------------------------------------------------------------------*/
529 
530 void
531 cs_sla_bwrite(const char *name,
532  const cs_sla_matrix_t *m,
533  const double *rhs,
534  const double *sol);
535 
536 /*----------------------------------------------------------------------------*/
546 /*----------------------------------------------------------------------------*/
547 
548 void
549 cs_sla_bread(const char *name,
550  cs_sla_matrix_t **p_mat,
551  double *p_rhs[],
552  double *p_sol[]);
553 
554 /*----------------------------------------------------------------------------*/
562 /*----------------------------------------------------------------------------*/
563 
564 void
565 cs_sla_matrix_summary(const char *name,
566  FILE *f,
567  cs_sla_matrix_t *m);
568 
569 /*----------------------------------------------------------------------------*/
577 /*----------------------------------------------------------------------------*/
578 
579 void
580 cs_sla_matrix_dump(const char *name,
581  FILE *f,
582  const cs_sla_matrix_t *m);
583 
584 /*----------------------------------------------------------------------------*/
593 /*----------------------------------------------------------------------------*/
594 
595 void
596 cs_sla_system_dump(const char *name,
597  FILE *f,
598  const cs_sla_matrix_t *m,
599  const double *rhs);
600 
601 /*----------------------------------------------------------------------------*/
615 /*----------------------------------------------------------------------------*/
616 
619  cs_lnum_t n_cells,
620  bool bktrans,
621  bool bk00sym,
622  const cs_adjacency_t *x2x,
623  const cs_adjacency_t *c2x);
624 
625 /*----------------------------------------------------------------------------*/
633 /*----------------------------------------------------------------------------*/
634 
637 
638 /*----------------------------------------------------------------------------*/
651 /*----------------------------------------------------------------------------*/
652 
653 void
655  const double vx[],
656  const double vc[],
657  double *iox[],
658  double *ioc[],
659  bool reset);
660 
661 /*----------------------------------------------------------------------------*/
662 
664 
665 #endif /* __CS_SLA_H__ */
double * cc_diag
Definition: cs_sla.h:118
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:64
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:89
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:85
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:105
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:113
double * xc_vals
Definition: cs_sla.h:120
#define BEGIN_C_DECLS
Definition: cs_defs.h:461
int stride
Definition: cs_sla.h:77
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:109
double precision, dimension(ncharm), save beta
Definition: cpincl.f90:99
cs_lnum_t * idx
Definition: cs_sla.h:81
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:110
short int * sgn
Definition: cs_sla.h:84
Definition: cs_sla.h:72
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_transpose(const cs_sla_matrix_t *a)
Transpose a cs_sla_matrix_t structure.
Definition: cs_sla.c:1541
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:79
int n_rows
Definition: cs_sla.h:78
Definition: cs_sla.h:66
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:74
Definition: cs_sla.h:68
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:107
cs_lnum_t n_rows
Definition: cs_sla.h:111
#define END_C_DECLS
Definition: cs_defs.h:462
cs_sla_matrix_type_t
Definition: cs_sla.h:62
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:65
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:75
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:117
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:67
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:87
cs_lnum_t * col_id
Definition: cs_sla.h:82
double * cx_vals
Definition: cs_sla.h:119
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