Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
Loading...
Searching...
No Matches
BlaSparseCheck.c
Go to the documentation of this file.
1
14#include <math.h>
15
16#include "fasp.h"
17#include "fasp_functs.h"
18
19/*---------------------------------*/
20/*-- Public Functions --*/
21/*---------------------------------*/
22
36{
37 const INT m = A->row;
38 INT i, num_neg;
39
40#if DEBUG_MODE > 1
41 printf("### DEBUG: nr = %d, nc = %d, nnz = %d\n", A->row, A->col, A->nnz);
42#endif
43
44 // store diagonal of A
45 dvector diag;
46 fasp_dcsr_getdiag(m,A,&diag);
47
48 // check positiveness of entries of diag
49 for (num_neg=i=0;i<m;++i) {
50 if (diag.val[i]<0) num_neg++;
51 }
52
53 printf("Number of negative diagonal entries = %d\n", num_neg);
54
55 fasp_dvec_free(&diag);
56
57 return num_neg;
58}
59
73{
74 const INT m = A->row;
75 const INT *ia = A->IA, *ja = A->JA;
76 const REAL *aj = A->val;
77
78 SHORT status = FASP_SUCCESS;
79 INT i,j,k,begin_row,end_row;
80
81 for ( i = 0; i < m; ++i ) {
82 begin_row = ia[i]; end_row = ia[i+1];
83 for ( k = begin_row; k < end_row; ++k ) {
84 j = ja[k];
85 if ( i == j ) {
86 if ( ABS(aj[k]) < SMALLREAL ) {
87 printf("### ERROR: diag[%d] = %e, close to zero!\n", i, aj[k]);
88 status = ERROR_DATA_ZERODIAG;
89 goto FINISHED;
90 }
91 }
92 } // end for k
93 } // end for i
94
95 FINISHED:
96 return status;
97}
98
115{
116 const INT nn = A->row;
117 const INT nnz = A->IA[nn]-A->IA[0];
118 INT i, j, k;
119 REAL sum;
120
121 INT *rowp = (INT *)fasp_mem_calloc(nnz,sizeof(INT));
122
123 for ( i=0; i<nn; ++i ) {
124 for ( j=A->IA[i]; j<A->IA[i+1]; ++j ) rowp[j]=i;
125 }
126
127 for ( k=0, i=0; i<nn; ++i ) {
128 sum = 0.0;
129 for ( j=A->IA[i]; j<A->IA[i+1]; ++j ) {
130 if ( A->JA[j]==i ) sum += A->val[j];
131 if ( A->JA[j]!=i ) sum -= fabs(A->val[j]);
132 }
133 if ( sum<-SMALLREAL ) ++k;
134 }
135
136 printf("Percentage of the diagonal-dominant rows is %3.2lf%s\n",
137 100.0*(REAL)(nn-k)/(REAL)nn,"%");
138
139 fasp_mem_free(rowp); rowp = NULL;
140
141 return k;
142}
143
160{
161 const REAL symmetry_tol = 1.0e-12;
162
163 INT *rowp,*rows[2],*cols[2];
164 INT i,j,mdi,mdj;
165 INT nns[2],tnizs[2];
166 INT type=0;
167
168 REAL maxdif,dif;
169 REAL *vals[2];
170
171 const INT nn = A->row;
172 const INT nnz = A->IA[nn]-A->IA[0];
173
174 if (nnz!=A->nnz) {
175 printf("### ERROR: nnz=%d, ia[n]-ia[0]=%d, mismatch!\n",A->nnz,nnz);
176 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
177 }
178
179 rowp=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
180
181 for (i=0;i<nn;++i) {
182 for (j=A->IA[i];j<A->IA[i+1];++j) rowp[j]=i;
183 }
184
185 rows[0]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
186 cols[0]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
187 vals[0]=(REAL *)fasp_mem_calloc(nnz,sizeof(REAL));
188
189 memcpy(rows[0],rowp,nnz*sizeof(INT));
190 memcpy(cols[0],A->JA,nnz*sizeof(INT));
191 memcpy(vals[0],A->val,nnz*sizeof(REAL));
192
193 nns[0]=nn;
194 nns[1]=A->col;
195 tnizs[0]=nnz;
196
197 rows[1]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
198 cols[1]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
199 vals[1]=(REAL *)fasp_mem_calloc(nnz,sizeof(REAL));
200
201 fasp_dcsr_transpose(rows,cols,vals,nns,tnizs);
202
203 memcpy(rows[0],rows[1],nnz*sizeof(INT));
204 memcpy(cols[0],cols[1],nnz*sizeof(INT));
205 memcpy(vals[0],vals[1],nnz*sizeof(REAL));
206 nns[0]=A->col;
207 nns[1]=nn;
208
209 fasp_dcsr_transpose(rows,cols,vals,nns,tnizs);
210
211 maxdif=0.;
212 mdi=0;
213 mdj=0;
214 for (i=0;i<nnz;++i) {
215 rows[0][i]=rows[1][i]-rows[0][i];
216 if (rows[0][i]!=0) {
217 type=-1;
218 mdi=rows[1][i];
219 break;
220 }
221
222 cols[0][i]=cols[1][i]-cols[0][i];
223 if (cols[0][i]!=0) {
224 type=-2;
225 mdj=cols[1][i];
226 break;
227 }
228
229 if (fabs(vals[0][i])>SMALLREAL||fabs(vals[1][i])>SMALLREAL) {
230 dif=fabs(vals[1][i]-vals[0][i])/(fabs(vals[0][i])+fabs(vals[1][i]));
231 if (dif>maxdif) {
232 maxdif=dif;
233 mdi=rows[0][i];
234 mdj=cols[0][i];
235 }
236 }
237 }
238
239 if (maxdif>symmetry_tol) type=-3;
240
241 switch (type) {
242 case 0:
243 printf("Matrix is symmetric with max relative difference is %1.3le\n",maxdif);
244 break;
245 case -1:
246 printf("Matrix has nonsymmetric pattern, check the %d-th, %d-th and %d-th rows and cols\n",
247 mdi-1,mdi,mdi+1);
248 break;
249 case -2:
250 printf("Matrix has nonsymmetric pattern, check the %d-th, %d-th and %d-th cols and rows\n",
251 mdj-1,mdj,mdj+1);
252 break;
253 case -3:
254 printf("Matrix is nonsymmetric with max relative difference is %1.3le\n",maxdif);
255 break;
256 default:
257 break;
258 }
259
260 fasp_mem_free(rowp); rowp = NULL;
261 fasp_mem_free(rows[0]); rows[0] = NULL;
262 fasp_mem_free(rows[1]); rows[1] = NULL;
263 fasp_mem_free(cols[0]); cols[0] = NULL;
264 fasp_mem_free(cols[1]); cols[1] = NULL;
265 fasp_mem_free(vals[0]); vals[0] = NULL;
266 fasp_mem_free(vals[1]); vals[1] = NULL;
267
268 return type;
269}
270
282{
283 INT i;
284
285 if ( (A->IA == NULL) || (A->JA == NULL) || (A->val == NULL) ) {
286 printf("### ERROR: Something is wrong with the matrix!\n");
287 fasp_chkerr(ERROR_MAT_SIZE, __FUNCTION__);
288 }
289
290 if ( A->row != A->col ) {
291 printf("### ERROR: Non-square CSR matrix!\n");
292 fasp_chkerr(ERROR_MAT_SIZE, __FUNCTION__);
293 }
294
295 if ( ( A->nnz <= 0 ) || ( A->row == 0 ) || ( A->col == 0 ) ) {
296 printf("### ERROR: Empty CSR matrix!\n");
297 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
298 }
299
300 for ( i = 0; i < A->nnz; ++i ) {
301 if ( ( A->JA[i] < 0 ) || ( A->JA[i] >= A->col ) ) {
302 printf("### ERROR: Wrong CSR matrix format!\n");
303 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
304 }
305 }
306}
307
319{
320 INT i;
321
322 if ( (A->IA == NULL) || (A->JA == NULL) || (A->val == NULL) ) {
323 printf("### ERROR: Something is wrong with the matrix!\n");
324 fasp_chkerr(ERROR_MAT_SIZE, __FUNCTION__);
325 }
326
327 if (A->row != A->col) {
328 printf("### ERROR: Non-square CSR matrix!\n");
329 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
330 }
331
332 if ( (A->nnz==0) || (A->row==0) || (A->col==0) ) {
333 printf("### ERROR: Empty CSR matrix!\n");
334 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
335 }
336
337 for (i=0;i<A->nnz;++i) {
338 if ( (A->JA[i]<0) || (A->JA[i]-A->col>=0) ) {
339 printf("### ERROR: Wrong CSR matrix format!\n");
340 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
341 }
342 }
343
344 return FASP_SUCCESS;
345}
346
358{
359 const INT n = A->col;
360 INT i, j, j1, j2, start, end;
361
362 for ( i=0; i<n; ++i ) {
363
364 start = A->IA[i];
365 end = A->IA[i+1] - 1;
366
367 for ( j=start; j<end-1; ++j ) {
368 j1 = A->JA[j]; j2 = A->JA[j + 1];
369 if ( j1 >= j2 ) {
370 printf("### ERROR: Order in row %10d is wrong! %10d, %10d\n", i, j1, j2);
371 fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
372 }
373 }
374
375 }
376
377}
378
379/*---------------------------------*/
380/*-- End of File --*/
381/*---------------------------------*/
void fasp_mem_free(void *mem)
Free up previous allocated memory body and set pointer to NULL.
Definition: AuxMemory.c:152
void * fasp_mem_calloc(const unsigned int size, const unsigned int type)
Allocate, initiate, and check memory.
Definition: AuxMemory.c:65
void fasp_chkerr(const SHORT status, const char *fctname)
Check error status and print out error messages before quit.
Definition: AuxMessage.c:213
void fasp_dvec_free(dvector *u)
Free vector data space of REAL type.
Definition: AuxVector.c:145
void fasp_dcsr_transpose(INT *row[2], INT *col[2], REAL *val[2], INT *nn, INT *tniz)
Transpose of a dCSRmat matrix.
void fasp_dcsr_getdiag(INT n, const dCSRmat *A, dvector *diag)
Get first n diagonal entries of a CSR matrix A.
Definition: BlaSparseCSR.c:537
void fasp_check_dCSRmat(const dCSRmat *A)
Check whether an dCSRmat matrix is supported or not.
INT fasp_check_diagdom(const dCSRmat *A)
Check whether a matrix is diagonally dominant.
SHORT fasp_check_iCSRmat(const iCSRmat *A)
Check whether an iCSRmat matrix is valid or not.
INT fasp_check_symm(const dCSRmat *A)
Check symmetry of a sparse matrix of CSR format.
INT fasp_check_diagpos(const dCSRmat *A)
Check positivity of diagonal entries of a CSR sparse matrix.
void fasp_check_ordering(dCSRmat *A)
Check whether each row of A is in ascending order w.r.t. column indices.
SHORT fasp_check_diagzero(const dCSRmat *A)
Check if a CSR sparse matrix has diagonal entries that are very close to zero.
Main header file for the FASP project.
#define REAL
Definition: fasp.h:75
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:71
#define ABS(a)
Definition: fasp.h:84
#define INT
Definition: fasp.h:72
#define FASP_SUCCESS
Definition of return status and error messages.
Definition: fasp_const.h:19
#define ERROR_DATA_STRUCTURE
Definition: fasp_const.h:31
#define ERROR_MAT_SIZE
Definition: fasp_const.h:26
#define ERROR_DATA_ZERODIAG
Definition: fasp_const.h:32
#define SMALLREAL
Definition: fasp_const.h:256
Sparse matrix of REAL type in CSR format.
Definition: fasp.h:151
INT col
column of matrix A, n
Definition: fasp.h:157
REAL * val
nonzero entries of A
Definition: fasp.h:169
INT row
row number of matrix A, m
Definition: fasp.h:154
INT * IA
integer array of row pointers, the size is m+1
Definition: fasp.h:163
INT nnz
number of nonzero entries
Definition: fasp.h:160
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:166
Vector with n entries of REAL type.
Definition: fasp.h:354
REAL * val
actual vector entries
Definition: fasp.h:360
Sparse matrix of INT type in CSR format.
Definition: fasp.h:190
INT col
column of matrix A, n
Definition: fasp.h:196
INT row
row number of matrix A, m
Definition: fasp.h:193
INT * IA
integer array of row pointers, the size is m+1
Definition: fasp.h:202
INT nnz
number of nonzero entries
Definition: fasp.h:199
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:205
INT * val
nonzero entries of A
Definition: fasp.h:208