Fast Auxiliary Space Preconditioning  1.8.4 Feb/15/2016
checkmat.c
Go to the documentation of this file.
1 
6 #include <math.h>
7 
8 #include "fasp.h"
9 #include "fasp_functs.h"
10 
11 /*---------------------------------*/
12 /*-- Public Functions --*/
13 /*---------------------------------*/
14 
28 {
29  const INT m=A->row;
30  unsigned INT i, num_neg;
31 
32 #if DEBUG_MODE > 1
33  printf("### DEBUG: nr = %d, nc = %d, nnz = %d\n", A->row, A->col, A->nnz);
34 #endif
35 
36  // store diagonal of A
37  dvector diag;
38  fasp_dcsr_getdiag(m,A,&diag);
39 
40  // check positiveness of entries of diag
41  for (num_neg=i=0;i<m;++i) {
42  if (diag.val[i]<0) num_neg++;
43  }
44 
45  printf("Number of negative diagonal entries = %d\n", num_neg);
46 
47  fasp_dvec_free(&diag);
48 
49  return num_neg;
50 }
51 
65 {
66  const INT m = A->row;
67  const INT *ia =A->IA, *ja = A->JA;
68  const REAL *aj =A->val;
69 
70  SHORT status = FASP_SUCCESS;
71  INT i,j,k,begin_row,end_row;
72 
73  for ( i = 0; i < m; ++i ) {
74  begin_row = ia[i], end_row = ia[i+1];
75  for ( k = begin_row; k < end_row; ++k ) {
76  j = ja[k];
77  if ( i == j ) {
78  if ( ABS(aj[k]) < SMALLREAL ) {
79  printf("### ERROR: diag[%d] = %e, close to zero!\n", i, aj[k]);
80  status = ERROR_DATA_ZERODIAG;
81  goto FINISHED;
82  }
83  }
84  } // end for k
85  } // end for i
86 
87  FINISHED:
88  return status;
89 }
90 
109 {
110  const INT nn = A->row;
111  const INT nnz = A->IA[nn]-A->IA[0];
112  INT i,j,k=0;
113  REAL sum;
114 
115  INT *rowp = (INT *)fasp_mem_calloc(nnz,sizeof(INT));
116 
117  for (i=0;i<nn;++i) {
118  for (j=A->IA[i];j<A->IA[i+1];++j) rowp[j]=i;
119  }
120 
121  for (i=0;i<nn;++i) {
122  sum=0.0;
123  for (j=A->IA[i];j<A->IA[i+1];++j) {
124  if (A->JA[j]==i) sum=sum+A->val[j];
125  if (A->JA[j]!=i) sum=sum-fabs(A->val[j]);
126  }
127  if (sum<-SMALLREAL) ++k;
128  }
129 
130  printf("Percentage of the diagonal-dominant rows is %3.2lf%s\n",
131  100.0*(REAL)(nn-k)/(REAL)nn,"%");
132 
133  fasp_mem_free(rowp);
134 
135  return k;
136 }
137 
154 {
155  const REAL symmetry_tol=1.0e-12;
156 
157  INT i,j,mdi,mdj,nnz,nn;
158  INT *rowp,*rows[2],*cols[2];
159  INT nns[2],tnizs[2];
160  INT type=0;
161 
162  REAL maxdif,dif;
163  REAL *vals[2];
164 
165  nn=A->row;
166  nnz=A->IA[nn]-A->IA[0];
167 
168  if (nnz!=A->nnz) {
169  printf("### ERROR: nnz=%d, ia[n]-ia[0]=%d does NOT match!\n",A->nnz,nnz);
170  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
171  }
172 
173  rowp=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
174 
175  for (i=0;i<nn;++i) {
176  for (j=A->IA[i];j<A->IA[i+1];++j) rowp[j]=i;
177  }
178 
179  rows[0]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
180  cols[0]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
181  vals[0]=(REAL *)fasp_mem_calloc(nnz,sizeof(REAL));
182 
183  memcpy(rows[0],rowp,nnz*sizeof(INT));
184  memcpy(cols[0],A->JA,nnz*sizeof(INT));
185  memcpy(vals[0],A->val,nnz*sizeof(REAL));
186 
187  nns[0]=nn;
188  nns[1]=A->col;
189  tnizs[0]=nnz;
190 
191  rows[1]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
192  cols[1]=(INT *)fasp_mem_calloc(nnz,sizeof(INT));
193  vals[1]=(REAL *)fasp_mem_calloc(nnz,sizeof(REAL));
194 
195  fasp_dcsr_transpose(rows,cols,vals,nns,tnizs);
196 
197  memcpy(rows[0],rows[1],nnz*sizeof(INT));
198  memcpy(cols[0],cols[1],nnz*sizeof(INT));
199  memcpy(vals[0],vals[1],nnz*sizeof(REAL));
200  nns[0]=A->col;
201  nns[1]=nn;
202 
203  fasp_dcsr_transpose(rows,cols,vals,nns,tnizs);
204 
205  maxdif=0.;
206  mdi=0;
207  mdj=0;
208  for (i=0;i<nnz;++i) {
209  rows[0][i]=rows[1][i]-rows[0][i];
210  if (rows[0][i]!=0) {
211  type=-1;
212  mdi=rows[1][i];
213  break;
214  }
215 
216  cols[0][i]=cols[1][i]-cols[0][i];
217  if (cols[0][i]!=0) {
218  type=-2;
219  mdj=cols[1][i];
220  break;
221  }
222 
223  if (fabs(vals[0][i])>SMALLREAL||fabs(vals[1][i])>SMALLREAL) {
224  dif=fabs(vals[1][i]-vals[0][i])/(fabs(vals[0][i])+fabs(vals[1][i]));
225  if (dif>maxdif) {
226  maxdif=dif;
227  mdi=rows[0][i];
228  mdj=cols[0][i];
229  }
230  }
231  }
232 
233  if (maxdif>symmetry_tol) type=-3;
234 
235  switch (type) {
236  case 0:
237  printf("Matrix is symmetric with max relative difference is %1.3le\n",maxdif);
238  break;
239  case -1:
240  printf("Matrix has nonsymmetric pattern, check the %d-th, %d-th and %d-th rows and cols\n",
241  mdi-1,mdi,mdi+1);
242  break;
243  case -2:
244  printf("Matrix has nonsymmetric pattern, check the %d-th, %d-th and %d-th cols and rows\n",
245  mdj-1,mdj,mdj+1);
246  break;
247  case -3:
248  printf("Matrix is nonsymmetric with max relative difference is %1.3le\n",maxdif);
249  break;
250  default:
251  break;
252  }
253 
254  fasp_mem_free(rowp);
255  fasp_mem_free(rows[1]);
256  fasp_mem_free(cols[1]);
257  fasp_mem_free(vals[1]);
258  fasp_mem_free(rows[0]);
259  fasp_mem_free(cols[0]);
260  fasp_mem_free(vals[0]);
261 
262  return type;
263 }
264 
276 {
277  INT i;
278 
279  if ( A->row != A->col ) {
280  printf("### ERROR: Non-square CSR matrix!\n");
281  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
282  }
283 
284  if ( (A->nnz==0) || (A->row==0) || (A->col==0) ) {
285  printf("### ERROR: Empty CSR matrix!\n");
286  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
287  }
288 
289  for (i=0;i<A->nnz;++i) {
290  if ( (A->JA[i]<0) || (A->JA[i]-A->col>=0) ) {
291  printf("### ERROR: Wrong CSR matrix format!\n");
292  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
293  }
294  }
295 
296  return FASP_SUCCESS;
297 }
298 
310 {
311  INT i;
312 
313  if (A->row != A->col) {
314  printf("### ERROR: Non-square CSR matrix!\n");
315  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
316  }
317 
318  if ( (A->nnz==0) || (A->row==0) || (A->col==0) ) {
319  printf("### ERROR: Empty CSR matrix!\n");
320  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
321  }
322 
323  for (i=0;i<A->nnz;++i) {
324  if ( (A->JA[i]<0) || (A->JA[i]-A->col>=0) ) {
325  printf("### ERROR: Wrong CSR matrix!\n");
326  fasp_chkerr(ERROR_DATA_STRUCTURE, __FUNCTION__);
327  }
328  }
329 
330  return FASP_SUCCESS;
331 }
332 
333 /*---------------------------------*/
334 /*-- End of File --*/
335 /*---------------------------------*/
void fasp_chkerr(const SHORT status, const char *fctname)
Check error status and print out error messages before quit.
Definition: message.c:199
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:163
#define REAL
Definition: fasp.h:67
#define ERROR_DATA_ZERODIAG
Definition: fasp_const.h:39
INT fasp_check_diagpos(dCSRmat *A)
Check positivity of diagonal entries of a CSR sparse matrix.
Definition: checkmat.c:27
INT fasp_check_diagdom(dCSRmat *A)
Check whether a matrix is diagonal dominant.
Definition: checkmat.c:108
INT fasp_check_symm(dCSRmat *A)
Check symmetry of a sparse matrix of CSR format.
Definition: checkmat.c:153
void fasp_dvec_free(dvector *u)
Free vector data space of REAL type.
Definition: vec.c:139
REAL * val
nonzero entries of A
Definition: fasp.h:166
REAL * val
actual vector entries
Definition: fasp.h:348
void * fasp_mem_calloc(LONGLONG size, INT type)
1M = 1024*1024
Definition: memory.c:60
Vector with n entries of REAL type.
Definition: fasp.h:342
SHORT fasp_check_iCSRmat(iCSRmat *A)
Check whether an iCSRmat matrix is valid or not.
Definition: checkmat.c:309
#define INT
Definition: fasp.h:64
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:193
#define FASP_SUCCESS
Definition of return status and error messages.
Definition: fasp_const.h:27
INT row
row number of matrix A, m
Definition: fasp.h:181
void fasp_mem_free(void *mem)
Free up previous allocated memory body.
Definition: memory.c:150
INT nnz
number of nonzero entries
Definition: fasp.h:157
INT col
column of matrix A, n
Definition: fasp.h:154
Main header file for FASP.
INT row
row number of matrix A, m
Definition: fasp.h:151
Sparse matrix of REAL type in CSR format.
Definition: fasp.h:148
void fasp_dcsr_getdiag(INT n, dCSRmat *A, dvector *diag)
Get first n diagonal entries of a CSR matrix A.
Definition: sparse_csr.c:410
#define ERROR_DATA_STRUCTURE
Definition: fasp_const.h:38
#define ABS(a)
Definition: fasp.h:74
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:63
SHORT fasp_check_dCSRmat(dCSRmat *A)
Check whether an dCSRmat matrix is valid or not.
Definition: checkmat.c:275
SHORT fasp_check_diagzero(dCSRmat *A)
Check wether a CSR sparse matrix has diagonal entries that are very close to zero.
Definition: checkmat.c:64
INT nnz
number of nonzero entries
Definition: fasp.h:187
INT col
column of matrix A, n
Definition: fasp.h:184
INT * IA
integer array of row pointers, the size is m+1
Definition: fasp.h:160
Sparse matrix of INT type in CSR format.
Definition: fasp.h:178
#define SMALLREAL
Definition: fasp_const.h:238