Fast Auxiliary Space Preconditioning  1.8.4 Feb/15/2016
io.c
Go to the documentation of this file.
1 
8 #include "fasp.h"
9 #include "fasp_functs.h"
10 #include "hb_io.h"
11 
12 // Flags which indicates lengths of INT and REAL numbers
16 // Decoration of private functions
17 static void fasp_dcsr_read_s(FILE *fp, dCSRmat *A);
18 static void fasp_dcsr_read_b(FILE *fp, dCSRmat *A, INT endianflag);
19 static void fasp_dcoo_read_s(FILE *fp, dCSRmat *A);
20 static void fasp_dcoo_read_b(FILE *fp, dCSRmat *A, INT endianflag);
21 static void fasp_dbsr_read_s(FILE *fp, dBSRmat *A);
22 static void fasp_dbsr_read_b(FILE *fp, dBSRmat *A, INT endianflag);
23 static void fasp_dstr_read_s(FILE *fp, dSTRmat *A);
24 static void fasp_dstr_read_b(FILE *fp, dSTRmat *A, INT endianflag);
25 static void fasp_dmtx_read_s(FILE *fp, dCSRmat *A);
26 static void fasp_dmtx_read_b(FILE *fp, dCSRmat *A, INT endianflag);
27 static void fasp_dmtxsym_read_s(FILE *fp, dCSRmat *A);
28 static void fasp_dmtxsym_read_b(FILE *fp, dCSRmat *A, INT endianflag);
29 static void fasp_dcsr_write_s(FILE *fp, dCSRmat *A);
30 static void fasp_dcsr_write_b(FILE *fp, dCSRmat *A);
31 static void fasp_dbsr_write_s(FILE *fp, dBSRmat *A);
32 static void fasp_dbsr_write_b(FILE *fp, dBSRmat *A);
33 static void fasp_dstr_write_s(FILE *fp, dSTRmat *A);
34 static void fasp_dstr_write_b(FILE *fp, dSTRmat *A);
35 static void fasp_dvec_read_s(FILE *fp, dvector *b);
36 static void fasp_dvec_read_b(FILE *fp, dvector *b, INT endianflag);
37 static void fasp_ivec_read_s(FILE *fp, ivector *b);
38 static void fasp_ivec_read_b(FILE *fp, ivector *b, INT endianflag);
39 static void fasp_dvecind_read_s(FILE *fp, dvector *b);
40 static void fasp_dvecind_read_b(FILE *fp, dvector *b, INT endianflag);
41 static void fasp_ivecind_read_s(FILE *fp, ivector *b);
42 static void fasp_ivecind_read_b(FILE *fp, ivector *b, INT endianflag);
43 static void fasp_dvec_write_s(FILE *fp, dvector *b);
44 static void fasp_dvec_write_b(FILE *fp, dvector *b);
45 static void fasp_ivec_write_s(FILE *fp, ivector *b);
46 static void fasp_ivec_write_b(FILE *fp, ivector *b);
47 static void fasp_dvecind_write_s(FILE *fp, dvector *b);
48 static void fasp_dvecind_write_b(FILE *fp, dvector *b);
49 static void fasp_ivecind_write_s(FILE *fp, ivector *b);
50 static void fasp_ivecind_write_b(FILE *fp, ivector *A);
51 
52 /*---------------------------------*/
53 /*-- Public Functions --*/
54 /*---------------------------------*/
55 
86 void fasp_dcsrvec1_read (const char *filename,
87  dCSRmat *A,
88  dvector *b)
89 {
90  int i,m,n,idata;
91  REAL ddata;
92 
93  // Open input disk file
94  FILE *fp = fopen(filename, "r");
95 
96  if (fasp_mem_check((void *)fp,NULL,ERROR_OPEN_FILE) < 0) {
97  printf("### ERROR: Cannot open %s!\n", filename);
98  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
99  }
100 
101  printf("%s: reading file %s...\n", __FUNCTION__, filename);
102 
103  // Read CSR matrix
104  fscanf(fp, "%d %d", &m, &n);
105  A->row=m; A->col=n;
106 
107  A->IA=(INT *)fasp_mem_calloc(m+1, sizeof(INT));
108  for ( i = 0; i <= m; ++i ) {
109  fscanf(fp, "%d", &idata);
110  A->IA[i] = idata;
111  }
112 
113  INT nnz = A->IA[m]-A->IA[0];
114 
115  A->nnz = nnz;
116  A->JA = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
117  A->val = (REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
118 
119  for ( i = 0; i < nnz; ++i ) {
120  fscanf(fp, "%d", &idata);
121  A->JA[i] = idata;
122  }
123 
124  for ( i = 0; i < nnz; ++i ) {
125  fscanf(fp, "%lf", &ddata);
126  A->val[i] = ddata;
127  }
128 
129  // Read RHS vector
130  fscanf(fp, "%d", &m);
131  b->row = m;
132 
133  b->val = (REAL*)fasp_mem_calloc(m, sizeof(REAL));
134 
135  for ( i = 0; i < m; ++i ) {
136  fscanf(fp, "%lf", &ddata);
137  b->val[i] = ddata;
138  }
139 
140  fclose(fp);
141 }
142 
178 void fasp_dcsrvec2_read (const char *filemat,
179  const char *filerhs,
180  dCSRmat *A,
181  dvector *b )
182 {
183  int i, n, tempi;
184 
185  /* read the matrix from file */
186  FILE *fp = fopen(filemat,"r");
187 
188  if ( fp == NULL ) {
189  printf("### ERROR: Cannot open %s!\n",filemat);
190  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
191  }
192 
193  printf("%s: reading file %s...\n", __FUNCTION__, filemat);
194 
195  fscanf(fp,"%d\n",&n);
196  A->row = n;
197  A->col = n;
198  A->IA = (INT *)fasp_mem_calloc(n+1, sizeof(INT));
199 
200  for ( i = 0; i <= n; ++i ) {
201  fscanf(fp,"%d\n",&tempi);
202  A->IA[i] = tempi-1;
203  }
204 
205  INT nz = A->IA[n];
206  A->nnz = nz;
207  A->JA = (INT *) fasp_mem_calloc(nz, sizeof(INT));
208  A->val = (REAL *)fasp_mem_calloc(nz, sizeof(REAL));
209 
210  for ( i = 0; i < nz; ++i ) {
211  fscanf(fp,"%d\n",&tempi);
212  A->JA[i] = tempi-1;
213  }
214 
215  for ( i = 0; i < nz; ++i ) fscanf(fp,"%le\n",&(A->val[i]));
216 
217  fclose(fp);
218 
219  /* Read the rhs from file */
220  b->row = n;
221  b->val = (REAL *)fasp_mem_calloc(n, sizeof(REAL));
222 
223  fp = fopen(filerhs,"r");
224 
225  if ( fp == NULL ) {
226  printf("### ERROR: Cannot open %s!\n",filerhs);
227  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
228  }
229 
230  printf("%s: reading file %s...\n", __FUNCTION__, filerhs);
231 
232  fscanf(fp,"%d\n",&n);
233 
234  if ( n != b->row ) {
235  printf("### ERROR: rhs size %d does not match matrix size %d!\n",n,b->row);
236  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
237  }
238 
239  for ( i = 0; i < n; ++i ) {
240  fscanf(fp,"%le\n", &(b->val[i]));
241  }
242 
243  fclose(fp);
244 }
245 
257 void fasp_dcsr_read (const char *filename,
258  dCSRmat *A)
259 {
260  int i,m,idata;
261  REAL ddata;
262 
263  // Open input disk file
264  FILE *fp = fopen(filename, "r");
265 
266  if (fasp_mem_check((void *)fp,NULL,ERROR_OPEN_FILE) < 0) {
267  printf("### ERROR: Cannot open %s!\n", filename);
268  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
269  }
270 
271  printf("%s: reading file %s...\n", __FUNCTION__, filename);
272 
273  // Read CSR matrix
274  fscanf(fp, "%d", &m);
275  A->row = A->col = m;
276 
277  A->IA = (INT *)fasp_mem_calloc(m+1, sizeof(INT));
278  for ( i = 0; i <= m; ++i ) {
279  fscanf(fp, "%d", &idata);
280  A->IA[i] = idata;
281  }
282 
283  INT nnz = A->IA[m]-A->IA[0];
284 
285  A->nnz = nnz;
286  A->JA = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
287  A->val = (REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
288 
289  for ( i = 0; i < nnz; ++i ) {
290  fscanf(fp, "%d", &idata);
291  A->JA[i] = idata;
292  }
293 
294  for ( i = 0; i < nnz; ++i ) {
295  fscanf(fp, "%lf", &ddata);
296  A->val[i]= ddata;
297  }
298  fclose(fp);
299 }
300 
318 void fasp_dcoo_read (const char *filename,
319  dCSRmat *A)
320 {
321  int i,j,k,m,n,nnz;
322  REAL value;
323 
324  FILE *fp = fopen(filename,"r");
325 
326  if ( fp == NULL ) {
327  printf("### ERROR: Cannot open %s!\n", filename);
328  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
329  }
330 
331  printf("%s: reading file %s...\n", __FUNCTION__, filename);
332 
333  fscanf(fp,"%d %d %d",&m,&n,&nnz);
334 
335  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
336 
337  for ( k = 0; k < nnz; k++ ) {
338  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
339  Atmp.rowind[k]=i; Atmp.colind[k]=j; Atmp.val[k] = value;
340  }
341  else {
342  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dcoo_read");
343  }
344  }
345 
346  fclose(fp);
347 
348  fasp_format_dcoo_dcsr(&Atmp,A);
349  fasp_dcoo_free(&Atmp);
350 }
351 
369 void fasp_dcoo1_read (const char *filename,
370  dCOOmat *A)
371 {
372  int i,j,k,m,n,nnz;
373  REAL value;
374 
375  FILE *fp = fopen(filename,"r");
376 
377  if ( fp == NULL ) {
378  printf("### ERROR: Cannot open %s!\n", filename);
379  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
380  }
381 
382  printf("%s: reading file %s...\n", __FUNCTION__, filename);
383 
384  fscanf(fp,"%d %d %d",&m,&n,&nnz);
385 
386  fasp_dcoo_alloc(m, n, nnz, A);
387 
388  for ( k = 0; k < nnz; k++ ) {
389  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
390  A->rowind[k]=i-1; A->colind[k]=j-1; A->val[k] = value;
391  }
392  else {
393  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dcoo1_read");
394  }
395  }
396 
397  fclose(fp);
398 
399 }
400 
420 void fasp_dcoo_shift_read (const char *filename,
421  dCSRmat *A)
422 {
423  int i,j,k,m,n,nnz;
424  REAL value;
425 
426  FILE *fp = fopen(filename,"r");
427 
428  if ( fp == NULL ) {
429  printf("### ERROR: Cannot open %s!\n", filename);
430  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
431  }
432 
433  printf("%s: reading file %s...\n", __FUNCTION__, filename);
434 
435  fscanf(fp,"%d %d %d",&m,&n,&nnz);
436 
437  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
438 
439  for ( k = 0; k < nnz; k++ ) {
440  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
441  Atmp.rowind[k]=i-1; Atmp.colind[k]=j-1; Atmp.val[k] = value;
442  }
443  else {
444  fasp_chkerr(ERROR_WRONG_FILE, __FUNCTION__);
445  }
446  }
447 
448  fclose(fp);
449 
450  fasp_format_dcoo_dcsr(&Atmp,A);
451  fasp_dcoo_free(&Atmp);
452 }
453 
472 void fasp_dmtx_read (const char *filename,
473  dCSRmat *A)
474 {
475  int i,j,m,n,nnz;
476  INT innz; // index of nonzeros
477  REAL value;
478 
479  FILE *fp = fopen(filename,"r");
480 
481  if ( fp == NULL ) {
482  printf("### ERROR: Cannot open %s!\n", filename);
483  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
484  }
485 
486  printf("%s: reading file %s...\n", __FUNCTION__, filename);
487 
488  fscanf(fp,"%d %d %d",&m,&n,&nnz);
489 
490  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
491 
492  innz = 0;
493 
494  while (innz < nnz) {
495  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
496 
497  Atmp.rowind[innz]=i-1;
498  Atmp.colind[innz]=j-1;
499  Atmp.val[innz] = value;
500  innz = innz + 1;
501 
502  }
503  else {
504  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtx_read");
505  }
506  }
507 
508  fclose(fp);
509 
510  fasp_format_dcoo_dcsr(&Atmp,A);
511  fasp_dcoo_free(&Atmp);
512 }
513 
534 void fasp_dmtxsym_read (const char *filename,
535  dCSRmat *A)
536 {
537  int i,j,m,n,nnz;
538  int innz; // index of nonzeros
539  REAL value;
540 
541  FILE *fp = fopen(filename,"r");
542 
543  if ( fp == NULL ) {
544  printf("### ERROR: Cannot open %s!\n", filename);
545  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
546  }
547 
548  printf("%s: reading file %s...\n", __FUNCTION__, filename);
549 
550  fscanf(fp,"%d %d %d",&m,&n,&nnz);
551 
552  nnz = 2*(nnz-m) + m; // adjust for sym problem
553 
554  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
555 
556  innz = 0;
557 
558  while (innz < nnz) {
559  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
560 
561  if ( i == j ) {
562  Atmp.rowind[innz] = i-1;
563  Atmp.colind[innz] = j-1;
564  Atmp.val[innz] = value;
565  innz = innz + 1;
566  }
567  else {
568  Atmp.rowind[innz] = i-1; Atmp.rowind[innz+1] = j-1;
569  Atmp.colind[innz] = j-1; Atmp.colind[innz+1] = i-1;
570  Atmp.val[innz] = value;
571  Atmp.val[innz+1] = value;
572  innz = innz + 2;
573  }
574 
575  }
576  else {
577  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtxsym_read");
578  }
579  }
580 
581  fclose(fp);
582 
583  fasp_format_dcoo_dcsr(&Atmp,A);
584  fasp_dcoo_free(&Atmp);
585 }
586 
611 void fasp_dstr_read (const char *filename,
612  dSTRmat *A)
613 {
614  int nx, ny, nz, nxy, ngrid, nband, nc, offset;
615  int i, k, n;
616  REAL value;
617 
618  FILE *fp = fopen(filename,"r");
619 
620  if ( fp == NULL ) {
621  printf("### ERROR: Cannot open %s!\n", filename);
622  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
623  }
624 
625  printf("%s: reading file %s...\n", __FUNCTION__, filename);
626 
627  fscanf(fp,"%d %d %d",&nx,&ny,&nz); // read dimension of the problem
628  A->nx = nx; A->ny = ny; A->nz = nz;
629 
630  nxy = nx*ny; ngrid = nxy*nz;
631  A->nxy = nxy; A->ngrid = ngrid;
632 
633  fscanf(fp,"%d",&nc); // read number of components
634  A->nc = nc;
635 
636  fscanf(fp,"%d",&nband); // read number of bands
637  A->nband = nband;
638 
639  A->offsets = (INT *)fasp_mem_calloc(nband, sizeof(INT));
640 
641  // read diagonal
642  fscanf(fp, "%d", &n);
643  A->diag = (REAL *)fasp_mem_calloc(n, sizeof(REAL));
644  for ( i = 0; i < n; ++i ) {
645  fscanf(fp, "%le", &value);
646  A->diag[i] = value;
647  }
648 
649  // read offdiags
650  k = nband;
651  A->offdiag = (REAL **)fasp_mem_calloc(nband, sizeof(REAL *));
652  while ( k-- ) {
653  fscanf(fp,"%d %d",&offset,&n); // read number band k
654  A->offsets[nband-k-1] = offset;
655 
656  A->offdiag[nband-k-1] = (REAL *)fasp_mem_calloc(n, sizeof(REAL));
657  for ( i = 0; i < n; ++i ) {
658  fscanf(fp, "%le", &value);
659  A->offdiag[nband-k-1][i] = value;
660  }
661  }
662 
663  fclose(fp);
664 }
665 
691 void fasp_dbsr_read (const char *filename,
692  dBSRmat *A)
693 {
694  int ROW, COL, NNZ, nb, storage_manner;
695  int i, n;
696  int index;
697  REAL value;
698 
699  FILE *fp = fopen(filename,"r");
700  if ( fp == NULL ) {
701  printf("### ERROR: Cannot open %s!\n", filename);
702  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
703  }
704 
705  printf("%s: reading file %s...\n", __FUNCTION__, filename);
706 
707  fscanf(fp, "%d %d %d", &ROW,&COL,&NNZ); // read dimension of the problem
708  A->ROW = ROW; A->COL = COL; A->NNZ = NNZ;
709 
710  fscanf(fp, "%d", &nb); // read the size of each block
711  A->nb = nb;
712 
713  fscanf(fp, "%d", &storage_manner); // read the storage_manner of each block
714  A->storage_manner = storage_manner;
715 
716  // allocate memory space
717  fasp_dbsr_alloc(ROW, COL, NNZ, nb, storage_manner, A);
718 
719  // read IA
720  fscanf(fp, "%d", &n);
721  for ( i = 0; i < n; ++i ) {
722  fscanf(fp, "%d", &index);
723  A->IA[i] = index;
724  }
725 
726  // read JA
727  fscanf(fp, "%d", &n);
728  for ( i = 0; i < n; ++i ){
729  fscanf(fp, "%d", &index);
730  A->JA[i] = index;
731  }
732 
733  // read val
734  fscanf(fp, "%d", &n);
735  for ( i = 0; i < n; ++i ) {
736  fscanf(fp, "%le", &value);
737  A->val[i] = value;
738  }
739 
740  fclose(fp);
741 }
742 
760 void fasp_dvecind_read (const char *filename,
761  dvector *b)
762 {
763  INT i, n;
764  INT index;
765  REAL value;
766  FILE *fp = fopen(filename,"r");
767 
768  if ( fp == NULL ) {
769  printf("### ERROR: Cannot open %s!\n", filename);
770  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
771  }
772 
773  printf("%s: reading file %s...\n", __FUNCTION__, filename);
774 
775  fscanf(fp,"%d",&n);
776  fasp_dvec_alloc(n,b);
777 
778  for ( i = 0; i < n; ++i ) {
779 
780  fscanf(fp, "%d %le", &index, &value);
781 
782  if ( value > BIGREAL || index >= n ) {
783  printf("### ERROR: Wrong index = %d or value = %lf\n", index, value);
784  fasp_dvec_free(b);
785  fclose(fp);
786  exit(ERROR_INPUT_PAR);
787  }
788 
789  b->val[index] = value;
790  }
791 
792  fclose(fp);
793 }
794 
810 void fasp_dvec_read (const char *filename,
811  dvector *b)
812 {
813 
814  int i, n;
815  REAL value;
816 
817  FILE *fp = fopen(filename,"r");
818  if ( fp == NULL ) {
819  printf("### ERROR: Cannot open %s!\n", filename);
820  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
821  }
822 
823  printf("%s: reading file %s...\n", __FUNCTION__, filename);
824 
825  fscanf(fp,"%d",&n);
826 
827  fasp_dvec_alloc(n,b);
828 
829  for ( i = 0; i < n; ++i ) {
830 
831  fscanf(fp, "%le", &value);
832  b->val[i] = value;
833 
834  if ( value > BIGREAL ) {
835  printf("### ERROR: Wrong value = %lf\n", value);
836  fasp_dvec_free(b);
837  fclose(fp);
838  exit(ERROR_INPUT_PAR);
839  }
840 
841 
842  }
843 
844  fclose(fp);
845 }
846 
862 void fasp_ivecind_read (const char *filename,
863  ivector *b)
864 {
865  int i, n, index, value;
866 
867  FILE *fp = fopen(filename,"r");
868 
869  if ( fp == NULL ) {
870  printf("### ERROR: Cannot open %s!\n", filename);
871  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
872  }
873 
874  printf("%s: reading file %s...\n", __FUNCTION__, filename);
875 
876  fscanf(fp,"%d",&n);
877  fasp_ivec_alloc(n,b);
878 
879  for ( i = 0; i < n; ++i ) {
880  fscanf(fp, "%d %d", &index, &value);
881  b->val[index] = value;
882  }
883 
884  fclose(fp);
885 }
886 
902 void fasp_ivec_read (const char *filename,
903  ivector *b)
904 {
905  int i, n, value;
906 
907  FILE *fp = fopen(filename,"r");
908 
909  if ( fp == NULL ) {
910  printf("### ERROR: Cannot open %s!\n", filename);
911  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
912 
913  }
914 
915  printf("%s: reading file %s...\n", __FUNCTION__, filename);
916 
917  fscanf(fp,"%d",&n);
918  fasp_ivec_alloc(n,b);
919 
920  for ( i = 0; i < n; ++i ) {
921  fscanf(fp, "%d", &value);
922  b->val[i] = value;
923  }
924 
925  fclose(fp);
926 }
927 
953 void fasp_dcsrvec1_write (const char *filename,
954  dCSRmat *A,
955  dvector *b)
956 {
957  INT m = A->row, n = A->col, nnz = A->nnz;
958  INT i;
959 
960  FILE *fp = fopen(filename, "w");
961 
962  /* write the matrix to file */
963  if ( fp == NULL ) {
964  printf("### ERROR: Cannot open %s!\n", filename);
965  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
966  }
967 
968  printf("%s: reading file %s...\n", __FUNCTION__, filename);
969 
970  fprintf(fp,"%d %d\n",m,n);
971  for ( i = 0; i < m+1; ++i ) {
972  fprintf(fp, "%d\n", A->IA[i]);
973  }
974  for (i = 0; i < nnz; ++i) {
975  fprintf(fp, "%d\n", A->JA[i]);
976  }
977  for (i = 0; i < nnz; ++i) {
978  fprintf(fp, "%le\n", A->val[i]);
979  }
980 
981  m = b->row;
982 
983  /* write the rhs to file */
984  if ( fp == NULL ) {
985  printf("### ERROR: Cannot open %s!\n", filename);
986  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
987  }
988 
989  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
990 
991  fprintf(fp,"%d\n",m);
992 
993  for ( i = 0; i < m; ++i ) fprintf(fp,"%le\n",b->val[i]);
994 
995  fclose(fp);
996 }
997 
1031 void fasp_dcsrvec2_write (const char *filemat,
1032  const char *filerhs,
1033  dCSRmat *A,
1034  dvector *b )
1035 {
1036  INT m=A->row, nnz=A->nnz;
1037  INT i;
1038 
1039  FILE *fp = fopen(filemat, "w");
1040 
1041  /* write the matrix to file */
1042  if ( fp == NULL ) {
1043  printf("### ERROR: Cannot open %s!\n", filemat);
1044  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1045  }
1046 
1047  printf("%s: writing to file %s...\n", __FUNCTION__, filemat);
1048 
1049  fprintf(fp,"%d\n",m);
1050  for ( i = 0; i < m+1; ++i ) {
1051  fprintf(fp, "%d\n", A->IA[i]+1);
1052  }
1053  for (i = 0; i < nnz; ++i) {
1054  fprintf(fp, "%d\n", A->JA[i]+1);
1055  }
1056  for (i = 0; i < nnz; ++i) {
1057  fprintf(fp, "%le\n", A->val[i]);
1058  }
1059 
1060  fclose(fp);
1061 
1062  m = b->row;
1063 
1064  fp = fopen(filerhs,"w");
1065 
1066  /* write the rhs to file */
1067  if ( fp == NULL ) {
1068  printf("### ERROR: Cannot open %s!\n", filerhs);
1069  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1070  }
1071 
1072  printf("%s: writing to file %s...\n", __FUNCTION__, filerhs);
1073 
1074  fprintf(fp,"%d\n",m);
1075 
1076  for ( i = 0; i < m; ++i ) fprintf(fp,"%le\n",b->val[i]);
1077 
1078  fclose(fp);
1079 }
1080 
1102 void fasp_dcoo_write (const char *filename,
1103  dCSRmat *A)
1104 {
1105  const INT m = A->row, n = A->col;
1106  INT i, j;
1107 
1108  FILE *fp = fopen(filename, "w");
1109 
1110  if ( fp == NULL ) {
1111  printf("### ERROR: Cannot open %s!\n", filename);
1112  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1113  }
1114 
1115  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1116 
1117  fprintf(fp,"%d %d %d\n",m,n,A->nnz);
1118  for ( i = 0; i < m; ++i ) {
1119  for ( j = A->IA[i]; j < A->IA[i+1]; j++ )
1120  fprintf(fp,"%d %d %0.15e\n",i,A->JA[j],A->val[j]);
1121  }
1122 
1123  fclose(fp);
1124 }
1125 
1142 void fasp_dstr_write (const char *filename,
1143  dSTRmat *A)
1144 {
1145  const INT nx = A->nx, ny = A->ny, nz = A->nz;
1146  const INT ngrid = A->ngrid, nband = A->nband, nc = A->nc;
1147 
1148  INT *offsets=A->offsets;
1149 
1150  unsigned INT i, k, n;
1151 
1152  FILE *fp = fopen(filename,"w");
1153  if ( fp == NULL ) {
1154  printf("### ERROR: Cannot open %s!\n", filename);
1155  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1156 
1157  }
1158 
1159  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1160 
1161  fprintf(fp,"%d %d %d\n",nx,ny,nz); // write dimension of the problem
1162 
1163  fprintf(fp,"%d\n",nc); // read number of components
1164 
1165  fprintf(fp,"%d\n",nband); // write number of bands
1166 
1167  // write diagonal
1168  n=ngrid*nc*nc; // number of nonzeros in each band
1169  fprintf(fp,"%d\n",n); // number of diagonal entries
1170  for ( i = 0; i < n; ++i ) fprintf(fp, "%le\n", A->diag[i]);
1171 
1172  // write offdiags
1173  k = nband;
1174  while ( k-- ) {
1175  INT offset=offsets[nband-k-1];
1176  n=(ngrid-ABS(offset))*nc*nc; // number of nonzeros in each band
1177  fprintf(fp,"%d %d\n",offset,n); // read number band k
1178  for ( i = 0; i < n; ++i ) {
1179  fprintf(fp, "%le\n", A->offdiag[nband-k-1][i]);
1180  }
1181  }
1182 
1183  fclose(fp);
1184 }
1185 
1202 void fasp_dbsr_write (const char *filename,
1203  dBSRmat *A)
1204 {
1205  const INT ROW = A->ROW, COL = A->COL, NNZ = A->NNZ;
1206  const INT nb = A->nb, storage_manner = A->storage_manner;
1207 
1208  INT *ia = A->IA;
1209  INT *ja = A->JA;
1210  REAL *val = A->val;
1211 
1212  unsigned INT i, n;
1213 
1214  FILE *fp = fopen(filename,"w");
1215  if ( fp == NULL ) {
1216  printf("### ERROR: Cannot open %s!\n", filename);
1217  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1218  }
1219 
1220  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1221 
1222  fprintf(fp,"%d %d %d\n",ROW,COL,NNZ); // write dimension of the block matrix
1223 
1224  fprintf(fp,"%d\n",nb); // write the size of each block
1225 
1226  fprintf(fp,"%d\n",storage_manner); // write storage manner of each block
1227 
1228  // write A->IA
1229  n = ROW+1; // length of A->IA
1230  fprintf(fp,"%d\n",n); // length of A->IA
1231  for ( i = 0; i < n; ++i ) fprintf(fp, "%d\n", ia[i]);
1232 
1233  // write A->JA
1234  n = NNZ; // length of A->JA
1235  fprintf(fp, "%d\n", n); // length of A->JA
1236  for ( i = 0; i < n; ++i ) fprintf(fp, "%d\n", ja[i]);
1237 
1238  // write A->val
1239  n = NNZ*nb*nb; // length of A->val
1240  fprintf(fp, "%d\n", n); // length of A->val
1241  for ( i = 0; i < n; ++i ) fprintf(fp, "%le\n", val[i]);
1242 
1243  fclose(fp);
1244 }
1245 
1257 void fasp_dvec_write (const char *filename,
1258  dvector *vec)
1259 {
1260  INT m = vec->row, i;
1261 
1262  FILE *fp = fopen(filename,"w");
1263 
1264  if ( fp == NULL ) {
1265  printf("### ERROR: Cannot open %s!\n", filename);
1266  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1267  }
1268 
1269  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1270 
1271  fprintf(fp,"%d\n",m);
1272 
1273  for ( i = 0; i < m; ++i ) fprintf(fp,"%0.15e\n",vec->val[i]);
1274 
1275  fclose(fp);
1276 }
1277 
1293 void fasp_dvecind_write (const char *filename,
1294  dvector *vec)
1295 {
1296  INT m = vec->row, i;
1297 
1298  FILE *fp = fopen(filename,"w");
1299  if ( fp == NULL ) {
1300  printf("### ERROR: Cannot open %s!\n", filename);
1301  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1302  }
1303 
1304  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1305 
1306  fprintf(fp,"%d\n",m);
1307 
1308  for ( i = 0; i < m; ++i ) fprintf(fp,"%d %le\n",i,vec->val[i]);
1309 
1310  fclose(fp);
1311 }
1312 
1328 void fasp_ivec_write (const char *filename,
1329  ivector *vec)
1330 {
1331  INT m = vec->row, i;
1332 
1333  FILE *fp = fopen(filename,"w");
1334 
1335  if ( fp == NULL ) {
1336  printf("### ERROR: Cannot open %s!\n", filename);
1337  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1338  }
1339 
1340  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1341 
1342  // write number of nonzeros
1343  fprintf(fp,"%d\n",m);
1344 
1345  // write index and value each line
1346  for ( i = 0; i < m; ++i ) fprintf(fp,"%d %d\n",i,vec->val[i]+1);
1347 
1348  fclose(fp);
1349 }
1350 
1363  dvector *u)
1364 {
1365  unsigned INT i;
1366 
1367  if ( n <= 0 ) n=u->row;
1368  for ( i = 0; i < n; ++i ) printf("vec_%d = %15.10E\n",i,u->val[i]);
1369 }
1370 
1383  ivector *u)
1384 {
1385  unsigned INT i;
1386 
1387  if ( n <= 0 ) n = u->row;
1388  for ( i = 0; i < n; ++i ) printf("vec_%d = %d\n",i,u->val[i]);
1389 }
1390 
1402 {
1403  const INT m=A->row, n=A->col;
1404  INT i, j;
1405 
1406  printf("nrow = %d, ncol = %d, nnz = %d\n",m,n,A->nnz);
1407  for ( i = 0; i < m; ++i ) {
1408  for (j=A->IA[i]; j<A->IA[i+1]; j++)
1409  printf("A_(%d,%d) = %+.10E\n",i,A->JA[j],A->val[j]);
1410  }
1411 }
1412 
1424 {
1425  INT k;
1426  printf("nrow = %d, ncol = %d, nnz = %d\n",A->row,A->col,A->nnz);
1427  for ( k = 0; k < A->nnz; k++ ) {
1428  printf("A_(%d,%d) = %+.10E\n",A->rowind[k],A->colind[k],A->val[k]);
1429  }
1430 }
1431 
1445 {
1446  INT i, j, k, l;
1447  INT nb,nb2;
1448  nb = A->nb;
1449  nb2 =nb*nb;
1450 
1451  printf("nrow = %d, ncol = %d, nnz = %d, nb = %d, storage_manner = %d\n",
1452  A->ROW, A->COL, A->NNZ, A->nb, A->storage_manner);
1453 
1454  for ( i = 0; i < A->ROW; i++ ) {
1455  for ( j = A->IA[i]; j < A->IA[i+1]; j++ ) {
1456  for ( k = 0; k < A->nb; k++ ) {
1457  for ( l = 0; l < A->nb; l++ ) {
1458  printf("A_(%d,%d) = %+.10E\n",
1459  i*nb+k+1, A->JA[j]*nb+l+1, A->val[ A->JA[j]*nb2+k*nb+l]);
1460  }
1461  }
1462  }
1463  }
1464 
1465 }
1466 
1467 
1481 void fasp_dbsr_write_coo (const char *filename,
1482  const dBSRmat *A)
1483 {
1484 
1485  INT i, j, k, l;
1486  INT nb,nb2;
1487  nb = A->nb;
1488  nb2 =nb*nb;
1489 
1490  FILE *fp = fopen(filename,"w");
1491  if ( fp == NULL ) {
1492  printf("### ERROR: Cannot open %s!\n", filename);
1493  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1494  }
1495 
1496 #if DEBUG_MODE > 1
1497  printf("nrow = %d, ncol = %d, nnz = %d, nb = %d, storage_manner = %d\n",
1498  A->ROW, A->COL, A->NNZ, A->nb, A->storage_manner);
1499 #endif
1500 
1501  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1502 
1503  fprintf(fp,"%% dimension of the block matrix and nonzeros %d %d %d\n",A->ROW,A->COL,A->NNZ); // write dimension of the block matrix
1504  fprintf(fp,"%% the size of each block %d\n",A->nb); // write the size of each block
1505  fprintf(fp,"%% storage manner of each block %d\n",A->storage_manner); // write storage manner of each block
1506 
1507  for ( i = 0; i < A->ROW; i++ ) {
1508  for ( j = A->IA[i]; j < A->IA[i+1]; j++ ) {
1509  for ( k = 0; k < A->nb; k++ ) {
1510  for ( l = 0; l < A->nb; l++ ) {
1511  fprintf(fp, "%d %d %+.10E\n",
1512  i*nb+k+1, A->JA[j]*nb+l+1, A->val[ j*nb2+k*nb+l]);
1513  }
1514  }
1515  }
1516  }
1517  fclose(fp);
1518 }
1519 
1531 void fasp_dcsr_write_coo (const char *filename,
1532  const dCSRmat *A)
1533 {
1534 
1535  INT i, j;
1536  printf("nrow = %d, ncol = %d, nnz = %d\n",
1537  A->row, A->col, A->nnz);
1538 
1539  FILE *fp = fopen(filename,"w");
1540  if ( fp == NULL ) {
1541  printf("### ERROR: Cannot open %s!\n", filename);
1542  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1543  }
1544 
1545  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1546 
1547  // write dimension of the block matrix
1548  fprintf(fp,"%% dimension of the block matrix and nonzeros %d %d %d\n",
1549  A->row,A->col,A->nnz);
1550 
1551  for ( i = 0; i < A->row; i++ ) {
1552  for ( j = A->IA[i]; j < A->IA[i+1]; j++ ) {
1553  fprintf(fp, "%d %d %+.10E\n", i+1, A->JA[j]+1, A->val[j]);
1554  }
1555  }
1556 
1557  fclose(fp);
1558 }
1559 
1571 {
1572  // To be added later! --Chensong
1573 }
1574 
1604 void fasp_matrix_read (const char *filename,
1605  void *A)
1606 {
1607 
1608  INT index,flag;
1609 
1610  FILE *fp = fopen(filename,"rb");
1611 
1612  if ( fp == NULL ) {
1613  printf("### ERROR: Cannot open %s!\n", filename);
1614  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1615  }
1616 
1617  printf("%s: reading file %s...\n", __FUNCTION__, filename);
1618 
1619  fread(&index, sizeof(INT), 1, fp);
1620 
1621  // matrix stored in ASCII format
1622  if ( index == 808464432 ) {
1623 
1624  fclose(fp);
1625  fp = fopen(filename,"r"); // reopen file of reading file in ASCII
1626  fscanf(fp,"%d\n",&flag); // jump over the first line
1627  fscanf(fp,"%d\n",&flag); // reading the format information
1628  flag = (INT) flag/100;
1629 
1630  switch (flag) {
1631  case 1:
1632  //fasp_dcsr_read_s(fp, (dCSRmat *)A);
1633  fasp_dcoo_read_s(fp, (dCSRmat *)A);
1634  break;
1635  case 2:
1636  fasp_dbsr_read_s(fp, (dBSRmat *)A);
1637  break;
1638  case 3:
1639  fasp_dstr_read_s(fp, (dSTRmat *)A);
1640  break;
1641  case 4:
1642  fasp_dcoo_read_s(fp, (dCSRmat *)A);
1643  break;
1644  case 5:
1645  fasp_dmtx_read_s(fp, (dCSRmat *)A);
1646  break;
1647  case 6:
1648  fasp_dmtxsym_read_s(fp, (dCSRmat *)A);
1649  break;
1650  default:
1651  printf("### ERROR: Unknown file flag %d", flag);
1652  }
1653 
1654  fclose(fp);
1655  return;
1656 
1657  }
1658 
1659  // matrix stored in binary format
1660 
1661  // judge endian's consistence of machine and file
1662  INT endianflag = index;
1663 
1664  fread(&index, sizeof(INT), 1, fp);
1665  index = endian_convert_int(index, sizeof(INT), endianflag);
1666  flag = (INT) index/100;
1667  ilength = (INT) (index - flag*100)/10;
1668  dlength = index%10;
1669 
1670  switch (flag) {
1671  case 1:
1672  fasp_dcsr_read_b(fp, (dCSRmat *)A, endianflag);
1673  break;
1674  case 2:
1675  fasp_dbsr_read_b(fp, (dBSRmat *)A, endianflag);
1676  break;
1677  case 3:
1678  fasp_dstr_read_b(fp, (dSTRmat *)A, endianflag);
1679  break;
1680  case 4:
1681  fasp_dcoo_read_b(fp, (dCSRmat *)A, endianflag);
1682  break;
1683  case 5:
1684  fasp_dmtx_read_b(fp, (dCSRmat *)A, endianflag);
1685  break;
1686  case 6:
1687  fasp_dmtxsym_read_b(fp, (dCSRmat *)A, endianflag);
1688  break;
1689  default:
1690  printf("### ERROR: Unknown file flag %d", flag);
1691  }
1692  fclose(fp);
1693 
1694 }
1695 
1709 void fasp_matrix_read_bin (const char *filename,
1710  void *A)
1711 {
1712  INT index, flag;
1713  FILE *fp = fopen(filename, "rb");
1714 
1715  if ( fp == NULL ) {
1716  printf("### ERROR: Cannot open %s!\n", filename);
1717  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1718  }
1719 
1720  printf("%s: reading file %s...\n", __FUNCTION__, filename);
1721 
1722  fread(&index, sizeof(INT), 1, fp);
1723 
1724  INT endianflag = 1;
1725 
1726  index = endian_convert_int(index, sizeof(INT), endianflag);
1727 
1728  flag = (INT) index/100;
1729  ilength = (int) (index - flag*100)/10;
1730  dlength = index%10;
1731 
1732  switch (flag) {
1733  case 1:
1734  fasp_dcoo_read_b(fp, (dCSRmat *)A, endianflag);
1735  break;
1736  case 2:
1737  fasp_dbsr_read_b(fp, (dBSRmat *)A, endianflag);
1738  break;
1739  case 3:
1740  fasp_dstr_read_b(fp, (dSTRmat *)A, endianflag);
1741  break;
1742  case 4:
1743  fasp_dcsr_read_b(fp, (dCSRmat *)A, endianflag);
1744  break;
1745  case 5:
1746  fasp_dmtx_read_b(fp, (dCSRmat *)A, endianflag);
1747  break;
1748  case 6:
1749  fasp_dmtxsym_read_b(fp, (dCSRmat *)A, endianflag);
1750  break;
1751  default:
1752  printf("### ERROR: Unknown file flag %d", flag);
1753  }
1754 
1755  fclose(fp);
1756 
1757 }
1758 
1783 void fasp_matrix_write (const char *filename,
1784  void *A,
1785  INT flag)
1786 {
1787 
1788  INT fileflag, matrixflag;
1789  FILE *fp;
1790 
1791  matrixflag = flag%100;
1792  fileflag = (INT) flag/100;
1793 
1794  // write matrix in ASCII file
1795  if( !fileflag ) {
1796 
1797  fp = fopen(filename,"w");
1798  if ( fp == NULL ) {
1799  printf("### ERROR: Cannot open %s!\n", filename);
1800  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1801  }
1802 
1803  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1804 
1805  fprintf(fp,"%d%d%d%d\n",fileflag,fileflag,fileflag,fileflag);
1806 
1807  fprintf(fp,"%d%d%d\n",matrixflag,(int)sizeof(INT),(int)sizeof(REAL));
1808 
1809  switch (matrixflag) {
1810  case 1:
1811  fasp_dcsr_write_s(fp, (dCSRmat *)A);
1812  break;
1813  case 2:
1814  fasp_dbsr_write_s(fp, (dBSRmat *)A);
1815  break;
1816  case 3:
1817  fasp_dstr_write_s(fp, (dSTRmat *)A);
1818  break;
1819  }
1820  fclose(fp);
1821  return;
1822  }
1823 
1824  // write matrix in binary file
1825  fp = fopen(filename,"wb");
1826 
1827  if ( fp == NULL ) {
1828  printf("### ERROR: Cannot open %s!\n", filename);
1829  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1830  }
1831 
1832  printf("%s: writing to file %s...\n", __FUNCTION__, filename);
1833 
1834  INT putflag = fileflag*100 + sizeof(INT)*10 + sizeof(REAL);
1835  fwrite(&putflag,sizeof(INT),1,fp);
1836 
1837  switch (matrixflag) {
1838  case 1:
1839  fasp_dcsr_write_b(fp, (dCSRmat *)A);
1840  break;
1841  case 2:
1842  fasp_dbsr_write_b(fp,(dBSRmat *)A);
1843  break;
1844  case 3:
1845  fasp_dstr_write_b(fp, (dSTRmat *)A);
1846  break;
1847  }
1848  fclose(fp);
1849 }
1850 
1876 void fasp_vector_read (const char *filerhs,
1877  void *b)
1878 {
1879 
1880  INT index,flag;
1881 
1882  FILE *fp = fopen(filerhs,"rb");
1883 
1884  if ( fp == NULL ) {
1885  printf("### ERROR: Cannot open %s!\n", filerhs);
1886  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1887  }
1888 
1889  printf("%s: reading file %s...\n", __FUNCTION__, filerhs);
1890 
1891  fread(&index, sizeof(INT), 1, fp);
1892 
1893  // vector stored in ASCII
1894  if (index==808464432) {
1895  fclose(fp);
1896  fp = fopen(filerhs,"r");
1897  fscanf(fp,"%d\n",&flag);
1898  fscanf(fp,"%d\n",&flag);
1899  flag = (int) flag/100;
1900 
1901  switch (flag) {
1902  case 1:
1903  fasp_dvec_read_s(fp, (dvector *)b);
1904  break;
1905  case 2:
1906  fasp_ivec_read_s(fp, (ivector *)b);
1907  break;
1908  case 3:
1909  fasp_dvecind_read_s(fp, (dvector *)b);
1910  break;
1911  case 4:
1912  fasp_ivecind_read_s(fp, (ivector *)b);
1913  break;
1914  }
1915  fclose(fp);
1916  return;
1917  }
1918 
1919  // vector stored in binary
1920  INT endianflag = index;
1921  fread(&index, sizeof(INT), 1, fp);
1922  index = endian_convert_int(index, sizeof(INT), endianflag);
1923  flag = (int) index/100;
1924  ilength = (int) (index-100*flag)/10;
1925  dlength = index%10;
1926 
1927  switch (flag) {
1928  case 1:
1929  fasp_dvec_read_b(fp, (dvector *)b, endianflag);
1930  break;
1931  case 2:
1932  fasp_ivec_read_b(fp, (ivector *)b, endianflag);
1933  break;
1934  case 3:
1935  fasp_dvecind_read_b(fp, (dvector *)b, endianflag);
1936  break;
1937  case 4:
1938  fasp_ivecind_read_b(fp, (ivector *)b, endianflag);
1939  break;
1940  }
1941  fclose(fp);
1942 }
1943 
1973 void fasp_vector_write (const char *filerhs,
1974  void *b,
1975  INT flag)
1976 {
1977 
1978  INT fileflag, vectorflag;
1979  FILE *fp;
1980 
1981  fileflag = (int) flag/10;
1982  vectorflag = (int) flag%10;
1983 
1984  // write vector in ASCII
1985  if (!fileflag) {
1986  fp = fopen(filerhs,"w");
1987 
1988  if ( fp == NULL ) {
1989  printf("### ERROR: Cannot open %s!\n", filerhs);
1990  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
1991  }
1992 
1993  printf("%s: writing to file %s...\n", __FUNCTION__, filerhs);
1994 
1995  fprintf(fp,"%d%d%d%d\n",fileflag,fileflag,fileflag,fileflag);
1996 
1997  fprintf(fp,"%d%d%d\n",vectorflag,(int)sizeof(INT),(int)sizeof(REAL));
1998 
1999  switch (vectorflag) {
2000  case 1:
2001  fasp_dvec_write_s(fp, (dvector *)b);
2002  break;
2003  case 2:
2004  fasp_ivec_write_s(fp, (ivector *)b);
2005  break;
2006  case 3:
2007  fasp_dvecind_write_s(fp, (dvector *)b);
2008  break;
2009  case 4:
2010  fasp_ivecind_write_s(fp, (ivector *)b);
2011  break;
2012  }
2013  fclose(fp);
2014  return;
2015  }
2016 
2017  // write vector in binary
2018  fp = fopen(filerhs,"wb");
2019 
2020  if ( fp == NULL ) {
2021  printf("### ERROR: Cannot open %s!\n", filerhs);
2022  fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
2023  }
2024 
2025  printf("%s: writing to file %s...\n", __FUNCTION__, filerhs);
2026 
2027  INT putflag = vectorflag*100 + sizeof(INT)*10 + sizeof(REAL);
2028  fwrite(&putflag,sizeof(INT),1,fp);
2029 
2030  switch (vectorflag) {
2031  case 1:
2032  fasp_dvec_write_b(fp, (dvector *)b);
2033  break;
2034  case 2:
2035  fasp_ivec_write_b(fp, (ivector *)b);
2036  break;
2037  case 3:
2038  fasp_dvecind_write_b(fp, (dvector *)b);
2039  break;
2040  case 4:
2041  fasp_ivecind_write_b(fp, (ivector *)b);
2042  break;
2043  }
2044  fclose(fp);
2045 }
2046 
2061 void fasp_hb_read (const char *input_file,
2062  dCSRmat *A,
2063  dvector *b)
2064 {
2065  //-------------------------
2066  // Setup local variables
2067  //-------------------------
2068  // variables for FASP
2069  dCSRmat tempA;
2070 
2071  // variables for hb_io
2072 
2073  int *colptr = NULL;
2074  double *exact = NULL;
2075  double *guess = NULL;
2076  int i;
2077  int indcrd;
2078  char *indfmt = NULL;
2079  FILE *input;
2080  int j;
2081  char *key = NULL;
2082  char *mxtype = NULL;
2083  int ncol;
2084  int neltvl;
2085  int nnzero;
2086  int nrhs;
2087  int nrhsix;
2088  int nrow;
2089  int ptrcrd;
2090  char *ptrfmt = NULL;
2091  int rhscrd;
2092  char *rhsfmt = NULL;
2093  int *rhsind = NULL;
2094  int *rhsptr = NULL;
2095  char *rhstyp = NULL;
2096  double *rhsval = NULL;
2097  double *rhsvec = NULL;
2098  int *rowind = NULL;
2099  char *title = NULL;
2100  int totcrd;
2101  int valcrd;
2102  char *valfmt = NULL;
2103  double *values = NULL;
2104 
2105  printf ( "\n" );
2106  printf ( "HB_FILE_READ reads all the data in an HB file.\n" );
2107 
2108  printf ( "\n" );
2109  printf ( "Reading the file '%s'\n", input_file );
2110 
2111  input = fopen ( input_file, "rt" );
2112 
2113  if ( !input )
2114  {
2115  printf ( "\n" );
2116  printf ( "### ERROR: Fail to open the file.\n" );
2117  return;
2118  }
2119 
2120  //-------------------------
2121  // Reading...
2122  //-------------------------
2123  hb_file_read ( input, &title, &key, &totcrd, &ptrcrd, &indcrd,
2124  &valcrd, &rhscrd, &mxtype, &nrow, &ncol, &nnzero, &neltvl,
2125  &ptrfmt, &indfmt, &valfmt, &rhsfmt, &rhstyp, &nrhs, &nrhsix,
2126  &colptr, &rowind, &values, &rhsval, &rhsptr, &rhsind, &rhsvec,
2127  &guess, &exact );
2128 
2129  //-------------------------
2130  // Printing if needed
2131  //-------------------------
2132 #if DEBUG_MODE > 1
2133  /*
2134  Print out the header information.
2135  */
2136  hb_header_print ( title, key, totcrd, ptrcrd, indcrd, valcrd,
2137  rhscrd, mxtype, nrow, ncol, nnzero, neltvl, ptrfmt, indfmt, valfmt,
2138  rhsfmt, rhstyp, nrhs, nrhsix );
2139  /*
2140  Print the structure information.
2141  */
2142  hb_structure_print ( ncol, mxtype, nnzero, neltvl, colptr, rowind );
2143 
2144  /*
2145  Print the values.
2146  */
2147  hb_values_print ( ncol, colptr, mxtype, nnzero, neltvl, values );
2148 
2149  if ( 0 < rhscrd )
2150  {
2151  /*
2152  Print a bit of the right hand sides.
2153  */
2154  if ( rhstyp[0] == 'F' )
2155  {
2156  r8mat_print_some ( nrow, nrhs, rhsval, 1, 1, 5, 5, " Part of RHS" );
2157  }
2158  else if ( rhstyp[0] == 'M' && mxtype[2] == 'A' )
2159  {
2160  i4vec_print_part ( nrhs+1, rhsptr, 10, " Part of RHSPTR" );
2161  i4vec_print_part ( nrhsix, rhsind, 10, " Part of RHSIND" );
2162  r8vec_print_part ( nrhsix, rhsvec, 10, " Part of RHSVEC" );
2163  }
2164  else if ( rhstyp[0] == 'M' && mxtype[2] == 'E' )
2165  {
2166  r8mat_print_some ( nnzero, nrhs, rhsval, 1, 1, 5, 5, " Part of RHS" );
2167  }
2168  /*
2169  Print a bit of the starting guesses.
2170  */
2171  if ( rhstyp[1] == 'G' )
2172  {
2173  r8mat_print_some ( nrow, nrhs, guess, 1, 1, 5, 5, " Part of GUESS" );
2174  }
2175  /*
2176  Print a bit of the exact solutions.
2177  */
2178  if ( rhstyp[2] == 'X' )
2179  {
2180  r8mat_print_some ( nrow, nrhs, exact, 1, 1, 5, 5, " Part of EXACT" );
2181  }
2182 
2183  }
2184 #endif
2185 
2186  //-------------------------
2187  // Closing
2188  //-------------------------
2189  fclose ( input );
2190 
2191  //-------------------------
2192  // Convert to FASP format
2193  //-------------------------
2194 
2195  // convert matrix
2196  if (ncol != nrow) {
2197  printf ( "### ERROR: The matrix is not square!\n" );
2198  goto FINISHED;
2199  }
2200 
2201  tempA = fasp_dcsr_create(nrow, ncol, nnzero);
2202 
2203  for (i=0; i<=ncol; i++) tempA.IA[i] = colptr[i]-1;
2204  for (i=0; i<nnzero; i++) tempA.JA[i] = rowind[i]-1;
2205  fasp_array_cp (nnzero, values, tempA.val);
2206 
2207  // if the matrix is symmeric
2208  if (mxtype[1] == 'S'){
2209 
2210  // A = A'+ A
2211  dCSRmat tempA_tran;
2212  fasp_dcsr_trans(&tempA, &tempA_tran);
2213  fasp_blas_dcsr_add(&tempA, 1.0, &tempA_tran, 1.0, A);
2214  fasp_dcsr_free(&tempA);
2215  fasp_dcsr_free(&tempA_tran);
2216 
2217  // modify diagonal entries
2218  for (i=0; i<A->row; i++) {
2219 
2220  for(j=A->IA[i]; j<A->IA[i+1]; j++){
2221 
2222  if (A->JA[j] == i) {
2223  A->val[j] = A->val[j]/2;
2224  break;
2225  }
2226 
2227  }
2228 
2229  }
2230 
2231  }
2232  // if the matrix is not symmetric
2233  else
2234  {
2235  fasp_dcsr_trans(&tempA, A);
2236  fasp_dcsr_free(&tempA);
2237 
2238  }
2239 
2240  // convert right hand side
2241 
2242  if ( nrhs == 0 ){
2243 
2244  printf ( "### ERROR: There is not right hand side!\n" );
2245  goto FINISHED;
2246 
2247  }
2248  else if (nrhs > 1){
2249 
2250  printf ( "### ERROR: There is more than one right hand side!\n" );
2251  goto FINISHED;
2252 
2253  }
2254  else {
2255 
2256  fasp_dvec_alloc(nrow, b);
2257  fasp_array_cp(nrow, rhsval, b->val);
2258  }
2259 
2260 
2261  //-------------------------
2262  // Cleanning
2263  //-------------------------
2264 FINISHED:
2265  if ( colptr )
2266  {
2267  free ( colptr );
2268  }
2269  if ( exact )
2270  {
2271  free ( exact );
2272  }
2273  if ( guess )
2274  {
2275  free ( guess );
2276  }
2277  if ( rhsind )
2278  {
2279  free ( rhsind );
2280  }
2281  if ( rhsptr )
2282  {
2283  free ( rhsptr );
2284  }
2285  if ( rhsval )
2286  {
2287  free ( rhsval );
2288  }
2289  if ( rhsvec )
2290  {
2291  free ( rhsvec );
2292  }
2293  if ( rowind )
2294  {
2295  free ( rowind );
2296  }
2297  if ( values )
2298  {
2299  free ( values );
2300  }
2301 
2302  return;
2303 
2304 
2305 }
2306 
2307 /*---------------------------------*/
2308 /*-- Private Functions --*/
2309 /*---------------------------------*/
2310 
2311 static void fasp_dcsr_read_s (FILE *fp,
2312  dCSRmat *A)
2313 {
2314  INT i,m,nnz,idata;
2315  REAL ddata;
2316 
2317  // Read CSR matrix
2318  fscanf(fp, "%d", &m);
2319  A->row=m;
2320 
2321  A->IA=(INT *)fasp_mem_calloc(m+1, sizeof(INT));
2322  for ( i = 0; i <= m; ++i ) {
2323  fscanf(fp, "%d", &idata);
2324  A->IA[i] = idata;
2325  }
2326 
2327  nnz=A->IA[m]-A->IA[0]; A->nnz=nnz;
2328 
2329  A->JA=(INT *)fasp_mem_calloc(nnz, sizeof(INT));
2330  A->val=(REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
2331 
2332  for ( i = 0; i < nnz; ++i ) {
2333  fscanf(fp, "%d", &idata);
2334  A->JA[i] = idata;
2335  }
2336 
2337  for ( i = 0; i < nnz; ++i ) {
2338  fscanf(fp, "%lf", &ddata);
2339  A->val[i]= ddata;
2340  }
2341 }
2342 
2343 static void fasp_dcsr_read_b (FILE *fp,
2344  dCSRmat *A,
2345  INT endianflag)
2346 {
2347  INT i,m,nnz,idata;
2348  REAL ddata;
2349 
2350  // Read CSR matrix
2351  fread(&idata, ilength, 1, fp);
2352  A->row = endian_convert_int(idata, ilength, endianflag);
2353  m = A->row;
2354 
2355  A->IA=(INT *)fasp_mem_calloc(m+1, sizeof(INT));
2356  for ( i = 0; i <= m; ++i ) {
2357  fread(&idata, ilength, 1, fp);
2358  A->IA[i] = endian_convert_int(idata, ilength, endianflag);
2359  }
2360 
2361  nnz=A->IA[m]-A->IA[0]; A->nnz=nnz;
2362 
2363  A->JA=(INT *)fasp_mem_calloc(nnz, sizeof(INT));
2364  A->val=(REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
2365 
2366  for ( i = 0; i < nnz; ++i ) {
2367  fread(&idata, ilength, 1, fp);
2368  A->JA[i] = endian_convert_int(idata, ilength, endianflag);
2369  }
2370 
2371  for ( i = 0; i < nnz; ++i ) {
2372  fread(&ddata, dlength, 1, fp);
2373  A->val[i] = endian_convert_real(ddata, dlength, endianflag);
2374  }
2375 }
2376 
2377 static void fasp_dcoo_read_s (FILE *fp,
2378  dCSRmat *A)
2379 {
2380  INT i,j,k,m,n,nnz;
2381  REAL value;
2382 
2383  fscanf(fp,"%d %d %d",&m,&n,&nnz);
2384 
2385  dCOOmat Atmp = fasp_dcoo_create(m,n,nnz);
2386 
2387  for ( k = 0; k < nnz; k++ ) {
2388  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
2389  Atmp.rowind[k]=i; Atmp.colind[k]=j; Atmp.val[k] = value;
2390  }
2391  else {
2392  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dcoo_read");
2393  }
2394  }
2395 
2396 
2397  fasp_format_dcoo_dcsr(&Atmp,A);
2398  fasp_dcoo_free(&Atmp);
2399 }
2400 
2401 static void fasp_dcoo_read_b (FILE *fp,
2402  dCSRmat *A,
2403  INT endianflag)
2404 {
2405  INT k,m,n,nnz,index;
2406  REAL value;
2407 
2408  fread(&m, ilength, 1, fp);
2409  m = endian_convert_int(m, ilength, endianflag);
2410  fread(&n, ilength, 1, fp);
2411  n = endian_convert_int(n, ilength, endianflag);
2412  fread(&nnz, ilength, 1, fp);
2413  nnz = endian_convert_int(nnz, ilength, endianflag);
2414 
2415  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
2416 
2417  for (k = 0; k < nnz; k++) {
2418  if ( fread(&index, ilength, 1, fp) !=EOF ) {
2419  Atmp.rowind[k] = endian_convert_int(index, ilength, endianflag);
2420  fread(&index, ilength, 1, fp);
2421  Atmp.colind[k] = endian_convert_int(index, ilength, endianflag);
2422  fread(&value, sizeof(REAL), 1, fp);
2423  Atmp.val[k] = endian_convert_real(value, sizeof(REAL), endianflag);
2424  }
2425  else {
2426  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dcoo_read");
2427  }
2428  }
2429 
2430  fasp_format_dcoo_dcsr(&Atmp, A);
2431  fasp_dcoo_free(&Atmp);
2432 }
2433 
2434 static void fasp_dbsr_read_s (FILE *fp,
2435  dBSRmat *A)
2436 {
2437  INT ROW, COL, NNZ, nb, storage_manner;
2438  INT i, n;
2439  INT index;
2440  REAL value;
2441 
2442  fscanf(fp, "%d %d %d", &ROW,&COL,&NNZ); // read dimension of the problem
2443  A->ROW = ROW; A->COL = COL; A->NNZ = NNZ;
2444 
2445  fscanf(fp, "%d", &nb); // read the size of each block
2446  A->nb = nb;
2447 
2448  fscanf(fp, "%d", &storage_manner); // read the storage_manner of each block
2449  A->storage_manner = storage_manner;
2450 
2451  // allocate memory space
2452  fasp_dbsr_alloc(ROW, COL, NNZ, nb, storage_manner, A);
2453 
2454  // read IA
2455  fscanf(fp, "%d", &n);
2456  for ( i = 0; i < n; ++i ) {
2457  fscanf(fp, "%d", &index);
2458  A->IA[i] = index;
2459  }
2460 
2461  // read JA
2462  fscanf(fp, "%d", &n);
2463  for ( i = 0; i < n; ++i ){
2464  fscanf(fp, "%d", &index);
2465  A->JA[i] = index;
2466  }
2467 
2468  // read val
2469  fscanf(fp, "%d", &n);
2470  for ( i = 0; i < n; ++i ) {
2471  fscanf(fp, "%le", &value);
2472  A->val[i] = value;
2473  }
2474 
2475 }
2476 
2477 static void fasp_dbsr_read_b (FILE *fp,
2478  dBSRmat *A,
2479  INT endianflag)
2480 {
2481  INT ROW, COL, NNZ, nb, storage_manner;
2482  INT i, n, index;
2483  REAL value;
2484 
2485  // read dimension of the problem
2486  fread(&ROW, ilength, 1, fp);
2487  A->ROW = endian_convert_int(ROW, ilength, endianflag);
2488  fread(&COL, ilength, 1, fp);
2489  A->COL = endian_convert_int(COL, ilength, endianflag);
2490  fread(&NNZ, ilength, 1, fp);
2491  A->NNZ = endian_convert_int(NNZ, ilength, endianflag);
2492 
2493  fread(&nb, ilength, 1, fp); // read the size of each block
2494  A->nb = endian_convert_int(nb, ilength, endianflag);
2495 
2496  fread(&storage_manner, 1, ilength, fp); // read the storage manner of each block
2497  A->storage_manner = endian_convert_int(storage_manner, ilength, endianflag);
2498 
2499  // allocate memory space
2500  fasp_dbsr_alloc(ROW, COL, NNZ, nb, storage_manner, A);
2501 
2502  // read IA
2503  fread(&n, ilength, 1, fp);
2504  for ( i = 0; i < n; i++ ) {
2505  fread(&index, 1, ilength, fp);
2506  A->IA[i] = endian_convert_int(index, ilength, endianflag);
2507  }
2508 
2509  // read JA
2510  fread(&n, ilength, 1, fp);
2511  for ( i = 0; i < n; i++ ) {
2512  fread(&index, ilength, 1, fp);
2513  A->JA[i] = endian_convert_int(index, ilength, endianflag);
2514  }
2515 
2516  // read val
2517  fread(&n, ilength, 1, fp);
2518  for ( i = 0; i < n; i++ ) {
2519  fread(&value, sizeof(REAL), 1, fp);
2520  A->val[i] = endian_convert_real(value, sizeof(REAL), endianflag);
2521  }
2522 }
2523 
2524 static void fasp_dstr_read_s (FILE *fp,
2525  dSTRmat *A)
2526 {
2527  INT nx, ny, nz, nxy, ngrid, nband, nc, offset;
2528  INT i, k, n;
2529  REAL value;
2530 
2531  fscanf(fp,"%d %d %d",&nx,&ny,&nz); // read dimension of the problem
2532  A->nx = nx; A->ny = ny; A->nz = nz;
2533 
2534  nxy = nx*ny; ngrid = nxy*nz;
2535  A->nxy = nxy; A->ngrid = ngrid;
2536 
2537  fscanf(fp,"%d",&nc); // read number of components
2538  A->nc = nc;
2539 
2540  fscanf(fp,"%d",&nband); // read number of bands
2541  A->nband = nband;
2542 
2543  A->offsets=(INT*)fasp_mem_calloc(nband, ilength);
2544 
2545  // read diagonal
2546  fscanf(fp, "%d", &n);
2547  A->diag=(REAL *)fasp_mem_calloc(n, sizeof(REAL));
2548  for ( i = 0; i < n; ++i ) {
2549  fscanf(fp, "%le", &value);
2550  A->diag[i] = value;
2551  }
2552 
2553  // read offdiags
2554  k = nband;
2555  A->offdiag=(REAL **)fasp_mem_calloc(nband, sizeof(REAL *));
2556  while ( k-- ) {
2557  fscanf(fp,"%d %d",&offset,&n); // read number band k
2558  A->offsets[nband-k-1]=offset;
2559 
2560  A->offdiag[nband-k-1]=(REAL *)fasp_mem_calloc(n, sizeof(REAL));
2561  for ( i = 0; i < n; ++i ) {
2562  fscanf(fp, "%le", &value);
2563  A->offdiag[nband-k-1][i] = value;
2564  }
2565  }
2566 
2567 }
2568 
2569 static void fasp_dstr_read_b (FILE *fp,
2570  dSTRmat *A,
2571  INT endianflag)
2572 {
2573  INT nx, ny, nz, nxy, ngrid, nband, nc, offset;
2574  INT i, k, n;
2575  REAL value;
2576 
2577  // read dimension of the problem
2578  fread(&nx, ilength, 1, fp);
2579  A->nx = endian_convert_int(nx, ilength, endianflag);
2580  fread(&ny, ilength, 1, fp);
2581  A->ny = endian_convert_int(ny, ilength, endianflag);
2582  fread(&nz, ilength, 1, fp);
2583  A->nz = endian_convert_int(nz, ilength, endianflag);
2584 
2585  nxy = nx*ny; ngrid = nxy*nz;
2586  A->nxy = nxy; A->ngrid = ngrid;
2587 
2588  // read number of components
2589  fread(&nc, ilength, 1, fp);
2590  A->nc = nc;
2591 
2592  // read number of bands
2593  fread(&nband, ilength, 1, fp);
2594  A->nband = nband;
2595 
2596  A->offsets=(INT*)fasp_mem_calloc(nband, ilength);
2597 
2598  // read diagonal
2599  fread(&n, ilength, 1, fp);
2600  n = endian_convert_int(n, ilength, endianflag);
2601  A->diag=(REAL *)fasp_mem_calloc(n, sizeof(REAL));
2602  for ( i = 0; i < n; i++ ) {
2603  fread(&value, sizeof(REAL), 1, fp);
2604  A->diag[i]=endian_convert_real(value, sizeof(REAL), endianflag);
2605  }
2606 
2607  // read offdiags
2608  k = nband;
2609  A->offdiag=(REAL **)fasp_mem_calloc(nband, sizeof(REAL *));
2610  while ( k-- ) {
2611  fread(&offset, ilength, 1, fp);
2612  A->offsets[nband-k-1]=endian_convert_int(offset, ilength, endianflag);;
2613 
2614  fread(&n, ilength, 1, fp);
2615  n = endian_convert_int(n, ilength, endianflag);
2616  A->offdiag[nband-k-1]=(REAL *)fasp_mem_calloc(n, sizeof(REAL));
2617  for ( i = 0; i < n; i++ ) {
2618  fread(&value, sizeof(REAL), 1, fp);
2619  A->offdiag[nband-k-1][i]=endian_convert_real(value, sizeof(REAL), endianflag);
2620  }
2621  }
2622 }
2623 
2624 static void fasp_dmtx_read_s (FILE *fp,
2625  dCSRmat *A)
2626 {
2627  INT i,j,m,n,nnz;
2628  INT innz; // index of nonzeros
2629  REAL value;
2630 
2631  fscanf(fp,"%d %d %d",&m,&n,&nnz);
2632 
2633  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
2634 
2635  innz = 0;
2636 
2637  while (innz < nnz) {
2638  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
2639 
2640  Atmp.rowind[innz]=i-1;
2641  Atmp.colind[innz]=j-1;
2642  Atmp.val[innz] = value;
2643  innz = innz + 1;
2644 
2645  }
2646  else {
2647  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtx_read_s");
2648  }
2649  }
2650 
2651  fasp_format_dcoo_dcsr(&Atmp,A);
2652  fasp_dcoo_free(&Atmp);
2653 }
2654 
2655 static void fasp_dmtx_read_b (FILE *fp,
2656  dCSRmat *A,
2657  INT endianflag)
2658 {
2659  INT m,n,k,nnz;
2660  INT index;
2661  REAL value;
2662 
2663  fread(&m, ilength, 1, fp);
2664  m = endian_convert_int(m, ilength, endianflag);
2665  fread(&n, ilength, 1, fp);
2666  n = endian_convert_int(n, ilength, endianflag);
2667  fread(&nnz, ilength, 1, fp);
2668  nnz = endian_convert_int(nnz, ilength, endianflag);
2669 
2670  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
2671 
2672 
2673  for (k = 0; k < nnz; k++) {
2674  if ( fread(&index, ilength, 1, fp) !=EOF ) {
2675  Atmp.rowind[k] = endian_convert_int(index, ilength, endianflag)-1;
2676  fread(&index, ilength, 1, fp);
2677  Atmp.colind[k] = endian_convert_int(index, ilength, endianflag)-1;
2678  fread(&value, sizeof(REAL), 1, fp);
2679  Atmp.val[k] = endian_convert_real(value, sizeof(REAL), endianflag);
2680  }
2681  else {
2682  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtx_read_b");
2683  }
2684  }
2685 
2686  fasp_format_dcoo_dcsr(&Atmp, A);
2687  fasp_dcoo_free(&Atmp);
2688 }
2689 
2690 static void fasp_dmtxsym_read_s (FILE *fp,
2691  dCSRmat *A)
2692 {
2693  INT i,j,m,n,nnz;
2694  INT innz; // index of nonzeros
2695  REAL value;
2696 
2697  fscanf(fp,"%d %d %d",&m,&n,&nnz);
2698 
2699  nnz = 2*(nnz-m) + m; // adjust for sym problem
2700 
2701  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
2702 
2703  innz = 0;
2704 
2705  while (innz < nnz) {
2706  if ( fscanf(fp, "%d %d %le", &i, &j, &value) != EOF ) {
2707 
2708  if (i==j) {
2709  Atmp.rowind[innz]=i-1;
2710  Atmp.colind[innz]=j-1;
2711  Atmp.val[innz] = value;
2712  innz = innz + 1;
2713  }
2714  else {
2715  Atmp.rowind[innz]=i-1; Atmp.rowind[innz+1]=j-1;
2716  Atmp.colind[innz]=j-1; Atmp.colind[innz+1]=i-1;
2717  Atmp.val[innz] = value; Atmp.val[innz+1] = value;
2718  innz = innz + 2;
2719  }
2720 
2721  }
2722  else {
2723  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtxsym_read_s");
2724  }
2725  }
2726 
2727  fasp_format_dcoo_dcsr(&Atmp,A);
2728  fasp_dcoo_free(&Atmp);
2729 }
2730 
2731 static void fasp_dmtxsym_read_b (FILE *fp,
2732  dCSRmat *A,
2733  INT endianflag)
2734 {
2735  INT m,n,nnz;
2736  INT innz;
2737  INT index[2];
2738  REAL value;
2739 
2740  fread(&m, ilength, 1, fp);
2741  m = endian_convert_int(m, ilength, endianflag);
2742  fread(&n, ilength, 1, fp);
2743  n = endian_convert_int(n, ilength, endianflag);
2744  fread(&nnz, ilength, 1, fp);
2745  nnz = endian_convert_int(nnz, ilength, endianflag);
2746 
2747  nnz = 2*(nnz-m) + m; // adjust for sym problem
2748 
2749  dCOOmat Atmp=fasp_dcoo_create(m,n,nnz);
2750 
2751  innz = 0;
2752 
2753  while (innz < nnz) {
2754  if ( fread(index, ilength, 2, fp) !=EOF ) {
2755 
2756  if (index[0]==index[1]) {
2757  INT indextemp = index[0];
2758  Atmp.rowind[innz] = endian_convert_int(indextemp, ilength, endianflag)-1;
2759  indextemp = index[1];
2760  Atmp.colind[innz] = endian_convert_int(indextemp, ilength, endianflag)-1;
2761  fread(&value, sizeof(REAL), 1, fp);
2762  Atmp.val[innz] = endian_convert_real(value, sizeof(REAL), endianflag);
2763  innz = innz + 1;
2764  }
2765  else {
2766  INT indextemp = index[0];
2767  Atmp.rowind[innz] = endian_convert_int(indextemp, ilength, endianflag)-1;
2768  Atmp.rowind[innz+1] = Atmp.rowind[innz];
2769  indextemp = index[1];
2770  Atmp.colind[innz] = endian_convert_int(indextemp, ilength, endianflag)-1;
2771  Atmp.colind[innz+1] = Atmp.colind[innz];
2772  fread(&value, sizeof(REAL), 1, fp);
2773  Atmp.val[innz] = endian_convert_real(value, sizeof(REAL), endianflag);
2774  Atmp.val[innz+1] = Atmp.val[innz];
2775  innz = innz + 2;
2776  }
2777 
2778  }
2779  else {
2780  fasp_chkerr(ERROR_WRONG_FILE, "fasp_dmtxsym_read_b");
2781  }
2782  }
2783 
2784  fasp_format_dcoo_dcsr(&Atmp,A);
2785  fasp_dcoo_free(&Atmp);
2786 }
2787 
2788 static void fasp_dcsr_write_s (FILE *fp,
2789  dCSRmat *A)
2790 {
2791  const INT m=A->row, n=A->col;
2792  INT i;
2793 
2794  fprintf(fp,"%d %d %d\n",m,n,A->nnz);
2795 
2796  for ( i = 0; i < m+1; ++i ) fprintf(fp,"%d\n", A->IA[i]);
2797 
2798  for ( i = 0; i < A->nnz; ++i ) fprintf(fp,"%d\n", A->JA[i]);
2799 
2800  for ( i = 0; i < A->nnz; ++i ) fprintf(fp,"%le\n", A->val[i]);
2801 }
2802 
2803 static void fasp_dcsr_write_b (FILE *fp,
2804  dCSRmat *A)
2805 {
2806  const INT m=A->row, n=A->col;
2807  INT i, j, nnz, index;
2808  REAL value;
2809 
2810  nnz = A->nnz;
2811  fwrite(&m, sizeof(INT), 1, fp);
2812  fwrite(&n, sizeof(INT), 1, fp);
2813  fwrite(&nnz, sizeof(INT), 1, fp);
2814  for ( i = 0; i < m; i++ ) {
2815  for (j = A->IA[i]; j < A->IA[i+1]; j++) {
2816  fwrite(&i, sizeof(INT), 1, fp);
2817  index = A->JA[j];
2818  value = A->val[j];
2819  fwrite(&index, sizeof(INT), 1, fp);
2820  fwrite(&value, sizeof(REAL), 1, fp);
2821  }
2822  }
2823 
2824  fclose(fp);
2825 }
2826 
2827 static void fasp_dbsr_write_s (FILE *fp,
2828  dBSRmat *A)
2829 {
2830  const INT ROW = A->ROW, COL = A->COL, NNZ = A->NNZ;
2831  const INT nb = A->nb, storage_manner = A->storage_manner;
2832 
2833  INT *ia = A->IA;
2834  INT *ja = A->JA;
2835  REAL *val = A->val;
2836 
2837  unsigned INT i, n;
2838 
2839  fprintf(fp,"%d %d %d\n",ROW,COL,NNZ); // write dimension of the block matrix
2840 
2841  fprintf(fp,"%d\n",nb); // write the size of each block
2842 
2843  fprintf(fp,"%d\n",storage_manner); // write storage manner of each block
2844 
2845  // write A->IA
2846  n = ROW+1; // length of A->IA
2847  fprintf(fp,"%d\n",n); // length of A->IA
2848  for ( i = 0; i < n; ++i ) fprintf(fp, "%d\n", ia[i]);
2849 
2850  // write A->JA
2851  n = NNZ; // length of A->JA
2852  fprintf(fp, "%d\n", n); // length of A->JA
2853  for ( i = 0; i < n; ++i ) fprintf(fp, "%d\n", ja[i]);
2854 
2855  // write A->val
2856  n = NNZ*nb*nb; // length of A->val
2857  fprintf(fp, "%d\n", n); // length of A->val
2858  for ( i = 0; i < n; ++i ) fprintf(fp, "%le\n", val[i]);
2859 
2860 }
2861 
2862 static void fasp_dbsr_write_b (FILE *fp,
2863  dBSRmat *A)
2864 {
2865  const INT ROW = A->ROW, COL = A->COL, NNZ = A->NNZ;
2866  const INT nb = A->nb, storage_manner = A->storage_manner;
2867 
2868  INT *ia = A->IA;
2869  INT *ja = A->JA;
2870  REAL *val = A->val;
2871 
2872  unsigned INT i, n, index;
2873  REAL value;
2874 
2875  // write dimension of the block matrix
2876  fwrite(&ROW, sizeof(INT), 1, fp);
2877  fwrite(&COL, sizeof(INT), 1, fp);
2878  fwrite(&NNZ, sizeof(INT), 1, fp);
2879 
2880  fwrite(&nb, sizeof(INT), 1, fp); // write the size of each block
2881 
2882  fwrite(&storage_manner, sizeof(INT), 1, fp);
2883 
2884  // write A.IA
2885  n = ROW+1;
2886  fwrite(&n, sizeof(INT), 1, fp);
2887  for ( i = 0; i < n; i++ ) {
2888  index = ia[i];
2889  fwrite(&index, sizeof(INT), 1, fp);
2890  }
2891 
2892  // write A.JA
2893  n = NNZ;
2894  fwrite(&n, sizeof(INT), 1, fp);
2895  for ( i = 0; i < n; i++ ) {
2896  index = ja[i];
2897  fwrite(&index, sizeof(INT), 1, fp);
2898  }
2899 
2900  // write A.val
2901  n = NNZ*nb*nb;
2902  fwrite(&n,sizeof(INT), 1, fp);
2903  for ( i = 0; i < n; i++ ) {
2904  value = val[i];
2905  fwrite(&value, sizeof(REAL), 1, fp);
2906  }
2907 }
2908 
2909 static void fasp_dstr_write_s (FILE *fp,
2910  dSTRmat *A)
2911 {
2912  const INT nx=A->nx, ny=A->ny, nz=A->nz;
2913  const INT ngrid=A->ngrid, nband=A->nband, nc=A->nc;
2914 
2915  INT *offsets=A->offsets;
2916 
2917  unsigned INT i, k, n;
2918 
2919  fprintf(fp,"%d %d %d\n",nx,ny,nz); // write dimension of the problem
2920 
2921  fprintf(fp,"%d\n",nc); // read number of components
2922 
2923  fprintf(fp,"%d\n",nband); // write number of bands
2924 
2925  // write diagonal
2926  n=ngrid*nc*nc; // number of nonzeros in each band
2927  fprintf(fp,"%d\n",n); // number of diagonal entries
2928  for ( i = 0; i < n; ++i ) fprintf(fp, "%le\n", A->diag[i]);
2929 
2930  // write offdiags
2931  k = nband;
2932  while ( k-- ) {
2933  INT offset=offsets[nband-k-1];
2934  n=(ngrid-ABS(offset))*nc*nc; // number of nonzeros in each band
2935  fprintf(fp,"%d %d\n",offset,n); // read number band k
2936  for ( i = 0; i < n; ++i ) {
2937  fprintf(fp, "%le\n", A->offdiag[nband-k-1][i]);
2938  }
2939  }
2940 
2941 }
2942 
2943 static void fasp_dstr_write_b (FILE *fp,
2944  dSTRmat *A)
2945 {
2946  const INT nx=A->nx, ny=A->ny, nz=A->nz;
2947  const INT ngrid=A->ngrid, nband=A->nband, nc=A->nc;
2948 
2949  INT *offsets=A->offsets;
2950 
2951  unsigned INT i, k, n;
2952  REAL value;
2953 
2954  // write dimension of the problem
2955  fwrite(&nx, sizeof(INT), 1, fp);
2956  fwrite(&ny, sizeof(INT), 1, fp);
2957  fwrite(&nz, sizeof(INT), 1, fp);
2958 
2959  fwrite(&nc, sizeof(INT), 1, fp); // read number of components
2960 
2961  fwrite(&nband, sizeof(INT), 1, fp); // write number of bands
2962 
2963  // write diagonal
2964  n=ngrid*nc*nc; // number of nonzeros in each band
2965  fwrite(&n, sizeof(INT), 1, fp); // number of diagonal entries
2966  for ( i = 0; i < n; i++ ) {
2967  value = A->diag[i];
2968  fwrite(&value, sizeof(REAL), 1, fp);
2969  }
2970 
2971  // write offdiags
2972  k = nband;
2973  while ( k-- ) {
2974  INT offset=offsets[nband-k-1];
2975  n=(ngrid-ABS(offset))*nc*nc; // number of nonzeros in each band
2976  fwrite(&offset, sizeof(INT), 1, fp);
2977  fwrite(&n, sizeof(INT), 1, fp);
2978  for ( i = 0; i < n; i++ ) {
2979  value = A->offdiag[nband-k-1][i];
2980  fwrite(&value, sizeof(REAL), 1, fp);
2981  }
2982  }
2983 
2984 }
2985 
2986 static void fasp_dvec_read_s (FILE *fp,
2987  dvector *b)
2988 {
2989 
2990  INT i, n;
2991  REAL value;
2992 
2993  fscanf(fp,"%d",&n);
2994  fasp_dvec_alloc(n,b);
2995 
2996  for ( i = 0; i < n; ++i ) {
2997  fscanf(fp, "%le", &value);
2998  b->val[i] = value;
2999  }
3000 }
3001 
3002 static void fasp_dvec_read_b (FILE *fp,
3003  dvector *b,
3004  INT endianflag)
3005 {
3006 
3007  INT i, n;
3008  REAL value;
3009 
3010  fread(&n, ilength, 1, fp);
3011  n = endian_convert_int(n, ilength, endianflag);
3012  fasp_dvec_alloc(n,b);
3013 
3014  for ( i = 0; i < n; i++ ) {
3015  fread(&value, dlength, 1, fp);
3016  b->val[i]=endian_convert_real(value, dlength, endianflag);
3017  }
3018 }
3019 
3020 static void fasp_ivec_read_s (FILE *fp,
3021  ivector *b)
3022 {
3023  INT i, n, value;
3024 
3025  fscanf(fp,"%d",&n);
3026  fasp_ivec_alloc(n,b);
3027 
3028  for ( i = 0; i < n; ++i ) {
3029  fscanf(fp, "%d", &value);
3030  b->val[i] = value;
3031  }
3032 }
3033 
3034 static void fasp_ivec_read_b (FILE *fp,
3035  ivector *b,
3036  INT endianflag)
3037 {
3038  INT i, n, value;
3039 
3040  fread(&n, ilength, 1, fp);
3041  n = endian_convert_int(n, ilength, endianflag);
3042  fasp_ivec_alloc(n,b);
3043 
3044  for ( i = 0; i < n; i++ ) {
3045  fread(&value, dlength, 1, fp);
3046  b->val[i]=endian_convert_real(value, dlength, endianflag);
3047  }
3048 
3049  fclose(fp);
3050 }
3051 
3052 static void fasp_dvecind_read_s (FILE *fp,
3053  dvector *b)
3054 {
3055  INT i, n, index;
3056  REAL value;
3057 
3058  fscanf(fp,"%d",&n);
3059  fasp_dvec_alloc(n,b);
3060 
3061  for ( i = 0; i < n; ++i ) {
3062  fscanf(fp, "%d %le", &index, &value);
3063  b->val[index] = value;
3064  }
3065 }
3066 
3067 static void fasp_dvecind_read_b (FILE *fp,
3068  dvector *b,
3069  INT endianflag)
3070 {
3071  INT i, n, index;
3072  REAL value;
3073 
3074  fread(&n, ilength, 1, fp);
3075  n = endian_convert_int(n, ilength, endianflag);
3076  fasp_dvec_alloc(n,b);
3077 
3078  for ( i = 0; i < n; i++ ) {
3079  fread(&index, ilength, 1, fp);
3080  fread(&value, dlength, 1, fp);
3081  index = endian_convert_int(index, ilength, endianflag);
3082  value = endian_convert_real(value, ilength, endianflag);
3083  b->val[index] = value;
3084  }
3085 }
3086 
3087 static void fasp_ivecind_read_s (FILE *fp,
3088  ivector *b)
3089 {
3090  INT i, n, index, value;
3091 
3092  fscanf(fp,"%d",&n);
3093  fasp_ivec_alloc(n,b);
3094 
3095  for ( i = 0; i < n; ++i ) {
3096  fscanf(fp, "%d %d", &index, &value);
3097  b->val[index] = value;
3098  }
3099 }
3100 
3101 static void fasp_ivecind_read_b (FILE *fp,
3102  ivector *b,
3103  INT endianflag)
3104 {
3105  INT i, n, index, value;
3106 
3107  fread(&n, ilength, 1, fp);
3108  n = endian_convert_int(n, ilength, endianflag);
3109  fasp_ivec_alloc(n,b);
3110 
3111  for ( i = 0; i < n; i++ ) {
3112  fread(&index, ilength, 1, fp);
3113  fread(&value, dlength, 1, fp);
3114  index = endian_convert_int(index, ilength, endianflag);
3115  value = endian_convert_real(value, dlength, endianflag);
3116  b->val[index] = value;
3117  }
3118 }
3119 
3120 static void fasp_dvec_write_s (FILE *fp,
3121  dvector *vec)
3122 {
3123  INT m = vec->row, i;
3124 
3125  fprintf(fp,"%d\n",m);
3126 
3127  for ( i = 0; i < m; ++i ) fprintf(fp,"%le\n",vec->val[i]);
3128 
3129 }
3130 
3131 static void fasp_dvec_write_b (FILE *fp,
3132  dvector *vec)
3133 {
3134  INT m = vec->row, i;
3135  REAL value;
3136 
3137  fwrite(&m, sizeof(INT), 1, fp);
3138 
3139  for ( i = 0; i < m; i++ ) {
3140  value = vec->val[i];
3141  fwrite(&value, sizeof(REAL), 1, fp);
3142  }
3143 }
3144 
3145 static void fasp_ivec_write_s (FILE *fp,
3146  ivector *vec)
3147 {
3148  INT m = vec->row, i;
3149 
3150  fprintf(fp,"%d\n",m);
3151 
3152  for ( i = 0; i < m; ++i ) fprintf(fp,"%d %d\n",i,vec->val[i]);
3153 
3154 }
3155 
3156 static void fasp_ivec_write_b (FILE *fp,
3157  ivector *vec)
3158 {
3159  INT m = vec->row, i, value;
3160 
3161  fwrite(&m, sizeof(INT), 1, fp);
3162 
3163  for ( i = 0; i < m; i++ ) {
3164  value = vec->val[i];
3165  fwrite(&i, sizeof(INT), 1, fp);
3166  fwrite(&value, sizeof(INT), 1, fp);
3167  }
3168 
3169 }
3170 
3171 static void fasp_dvecind_write_s (FILE *fp,
3172  dvector *vec)
3173 {
3174  INT m = vec->row, i;
3175 
3176  fprintf(fp,"%d\n",m);
3177 
3178  for ( i = 0; i < m; ++i ) fprintf(fp,"%d %le\n",i,vec->val[i]);
3179 
3180 }
3181 
3182 static void fasp_dvecind_write_b (FILE *fp,
3183  dvector *vec)
3184 {
3185  INT m = vec->row, i;
3186  REAL value;
3187 
3188  fwrite(&m, sizeof(INT), 1, fp);
3189 
3190  for ( i = 0; i < m; i++ ) {
3191  value = vec->val[i];
3192  fwrite(&i, sizeof(INT), 1, fp);
3193  fwrite(&value, sizeof(REAL), 1, fp);
3194  }
3195 
3196 }
3197 
3198 static void fasp_ivecind_write_b (FILE *fp,
3199  ivector *vec)
3200 {
3201  INT m = vec->row, i;
3202  INT value;
3203 
3204  fwrite(&m, sizeof(INT), 1, fp);
3205 
3206  for ( i = 0; i < m; i++ ) {
3207  value = vec->val[i];
3208  fwrite(&i, sizeof(INT), 1, fp);
3209  fwrite(&value, sizeof(INT), 1, fp);
3210  }
3211 
3212 }
3213 
3214 static void fasp_ivecind_write_s (FILE *fp,
3215  ivector *vec)
3216 {
3217  INT m = vec->row, i;
3218 
3219  fprintf(fp,"%d\n",m);
3220 
3221  for ( i = 0; i < m; ++i ) fprintf(fp, "%d %d\n", i, vec->val[i]);
3222 
3223 }
3224 
3225 /*---------------------------------*/
3226 /*-- End of File --*/
3227 /*---------------------------------*/
3228 
INT nx
number of grids in x direction
Definition: fasp.h:307
dCOOmat fasp_dcoo_create(const INT m, const INT n, const INT nnz)
Create IJ sparse matrix data memory space.
Definition: sparse_coo.c:34
void fasp_dcoo_write(const char *filename, dCSRmat *A)
Write a matrix to disk file in IJ format (coordinate format)
Definition: io.c:1102
dCSRmat fasp_dcsr_create(const INT m, const INT n, const INT nnz)
Create CSR sparse matrix data memory space.
Definition: sparse_csr.c:34
INT * rowind
integer array of row indices, the size is nnz
Definition: fasp.h:221
void fasp_chkerr(const SHORT status, const char *fctname)
Check error status and print out error messages before quit.
Definition: message.c:199
#define ERROR_OPEN_FILE
Definition: fasp_const.h:29
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:163
#define ERROR_INPUT_PAR
Definition: fasp_const.h:31
#define REAL
Definition: fasp.h:67
Vector with n entries of INT type.
Definition: fasp.h:356
INT ngrid
number of grids
Definition: fasp.h:322
void fasp_ivec_write(const char *filename, ivector *vec)
Write a ivector to disk file in coordinate format.
Definition: io.c:1328
INT nc
size of each block (number of components)
Definition: fasp.h:319
void fasp_dcsr_write_coo(const char *filename, const dCSRmat *A)
Print out a dCSRmat matrix in coordinate format for matlab spy.
Definition: io.c:1531
void fasp_dvecind_read(const char *filename, dvector *b)
Read b from matrix disk file.
Definition: io.c:760
INT fasp_dcsr_trans(dCSRmat *A, dCSRmat *AT)
Find transpose of dCSRmat matrix A.
Definition: sparse_csr.c:826
void fasp_dvecind_write(const char *filename, dvector *vec)
Write a dvector to disk file in coordinate format.
Definition: io.c:1293
INT nb
dimension of each sub-block
Definition: fasp_block.h:56
REAL ** offdiag
off-diagonal entries (dimension is nband * [(ngrid-|offsets|) * nc^2])
Definition: fasp.h:334
void fasp_dstr_write(const char *filename, dSTRmat *A)
Write a dSTRmat to a disk file.
Definition: io.c:1142
INT * offsets
offsets of the off-diagonals (length is nband)
Definition: fasp.h:331
void fasp_dmtxsym_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in MatrixMarket sym format.
Definition: io.c:534
Structure matrix of REAL type.
Definition: fasp.h:304
void fasp_dvec_free(dvector *u)
Free vector data space of REAL type.
Definition: vec.c:139
INT endian_convert_int(const INT inum, const INT ilength, const INT endianflag)
Swap order of an INT number.
Definition: convert.c:105
REAL * val
nonzero entries of A
Definition: fasp.h:166
INT nz
number of grids in z direction
Definition: fasp.h:313
REAL * val
nonzero entries of A
Definition: fasp.h:227
void fasp_ivec_print(INT n, ivector *u)
Print first n entries of a vector of INT type.
Definition: io.c:1382
Block sparse row storage matrix of REAL type.
Definition: fasp_block.h:44
REAL * val
actual vector entries
Definition: fasp.h:348
INT col
column of matrix A, n
Definition: fasp.h:215
void * fasp_mem_calloc(LONGLONG size, INT type)
1M = 1024*1024
Definition: memory.c:60
void fasp_dbsr_write(const char *filename, dBSRmat *A)
Write a dBSRmat to a disk file.
Definition: io.c:1202
Vector with n entries of REAL type.
Definition: fasp.h:342
#define ERROR_WRONG_FILE
Definition: fasp_const.h:30
INT nnz
number of nonzero entries
Definition: fasp.h:218
void fasp_dvec_write(const char *filename, dvector *vec)
Write a dvector to disk file.
Definition: io.c:1257
#define INT
Definition: fasp.h:64
void fasp_dcoo_alloc(const INT m, const INT n, const INT nnz, dCOOmat *A)
Allocate COO sparse matrix memory space.
Definition: sparse_coo.c:62
INT nband
number of off-diag bands
Definition: fasp.h:328
INT row
row number of matrix A, m
Definition: fasp.h:212
Sparse matrix of REAL type in COO (or IJ) format.
Definition: fasp.h:209
void fasp_dbsr_write_coo(const char *filename, const dBSRmat *A)
Print out a dBSRmat matrix in coordinate format for matlab spy.
Definition: io.c:1481
INT fasp_blas_dcsr_add(dCSRmat *A, const REAL alpha, dCSRmat *B, const REAL beta, dCSRmat *C)
compute C = alpha*A + beta*B in CSR format
Definition: blas_csr.c:48
void fasp_dbsr_print(dBSRmat *A)
Print out a dBSRmat matrix in coordinate format.
Definition: io.c:1444
void fasp_dcsrvec1_read(const char *filename, dCSRmat *A, dvector *b)
Read A and b from a SINGLE disk file.
Definition: io.c:86
REAL * val
Definition: fasp_block.h:67
INT NNZ
number of nonzero sub-blocks in matrix A, NNZ
Definition: fasp_block.h:53
void fasp_dstr_read(const char *filename, dSTRmat *A)
Read A from a disk file in dSTRmat format.
Definition: io.c:611
void fasp_dmtx_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in MatrixMarket general format.
Definition: io.c:472
void fasp_dcoo_free(dCOOmat *A)
Free IJ sparse matrix data memory space.
Definition: sparse_coo.c:94
INT nnz
number of nonzero entries
Definition: fasp.h:157
INT col
column of matrix A, n
Definition: fasp.h:154
INT storage_manner
storage manner for each sub-block
Definition: fasp_block.h:59
void fasp_matrix_write(const char *filename, void *A, INT flag)
write matrix from different kinds of formats from both ASCII and binary files
Definition: io.c:1783
Main header file for FASP.
void fasp_dvec_read(const char *filename, dvector *b)
Read b from a disk file in array format.
Definition: io.c:810
void fasp_dcsrvec1_write(const char *filename, dCSRmat *A, dvector *b)
Write A and b to a SINGLE disk file.
Definition: io.c:953
INT * val
actual vector entries
Definition: fasp.h:362
void fasp_vector_read(const char *filerhs, void *b)
Read RHS vector from different kinds of formats from both ASCII and binary files. ...
Definition: io.c:1876
SHORT fasp_format_dcoo_dcsr(dCOOmat *A, dCSRmat *B)
Transform a REAL matrix from its IJ format to its CSR format.
Definition: formats.c:27
SHORT fasp_mem_check(void *ptr, const char *message, INT ERR)
Check wether a point is null or not.
Definition: memory.c:197
void fasp_dcsrvec2_read(const char *filemat, const char *filerhs, dCSRmat *A, dvector *b)
Read A and b from two disk files.
Definition: io.c:178
void fasp_dbsr_alloc(const INT ROW, const INT COL, const INT NNZ, const INT nb, const INT storage_manner, dBSRmat *A)
Allocate memory space for BSR format sparse matrix.
Definition: sparse_bsr.c:87
INT row
row number of matrix A, m
Definition: fasp.h:151
void fasp_hb_read(const char *input_file, dCSRmat *A, dvector *b)
Read matrix and right-hans side from a HB format file.
Definition: io.c:2061
void fasp_dcoo_shift_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format – indices starting from 0.
Definition: io.c:420
Sparse matrix of REAL type in CSR format.
Definition: fasp.h:148
void fasp_vector_write(const char *filerhs, void *b, INT flag)
write RHS vector from different kinds of formats in both ASCII and binary files
Definition: io.c:1973
void fasp_dcsrvec2_write(const char *filemat, const char *filerhs, dCSRmat *A, dvector *b)
Write A and b to two disk files.
Definition: io.c:1031
INT row
number of rows
Definition: fasp.h:345
REAL * diag
diagonal entries (length is ngrid*(nc^2))
Definition: fasp.h:325
INT COL
number of cols of sub-blocks in matrix A, N
Definition: fasp_block.h:50
void fasp_ivec_alloc(const INT m, ivector *u)
Create vector data space of INT type.
Definition: vec.c:119
void fasp_matrix_read_bin(const char *filename, void *A)
Read matrix in binary format.
Definition: io.c:1709
#define ABS(a)
Definition: fasp.h:74
void fasp_dstr_print(dSTRmat *A)
Print out a dSTRmat matrix in coordinate format.
Definition: io.c:1570
void fasp_dcoo_print(dCOOmat *A)
Print out a dCOOmat matrix in coordinate format.
Definition: io.c:1423
void fasp_dcoo_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format – indices starting from 0.
Definition: io.c:318
INT nxy
number of grids on x-y plane
Definition: fasp.h:316
INT ilength
Definition: io.c:13
void fasp_matrix_read(const char *filename, void *A)
Read matrix from different kinds of formats from both ASCII and binary files.
Definition: io.c:1604
INT dlength
Definition: io.c:14
void fasp_dcsr_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format.
Definition: io.c:257
void fasp_dbsr_read(const char *filename, dBSRmat *A)
Read A from a disk file in dBSRmat format.
Definition: io.c:691
INT * colind
integer array of column indices, the size is nnz
Definition: fasp.h:224
#define BIGREAL
Some global constants.
Definition: fasp_const.h:237
INT row
number of rows
Definition: fasp.h:359
void fasp_dcoo1_read(const char *filename, dCOOmat *A)
Read A from matrix disk file in IJ format – indices starting from 1.
Definition: io.c:369
void fasp_dvec_alloc(const INT m, dvector *u)
Create dvector data space of REAL type.
Definition: vec.c:99
INT ROW
number of rows of sub-blocks in matrix A, M
Definition: fasp_block.h:47
void fasp_array_cp(const INT n, REAL *x, REAL *y)
Copy an array to the other y=x.
Definition: array.c:165
INT * JA
Definition: fasp_block.h:74
void fasp_dcsr_print(dCSRmat *A)
Print out a dCSRmat matrix in coordinate format.
Definition: io.c:1401
void fasp_ivec_read(const char *filename, ivector *b)
Read b from a disk file in array format.
Definition: io.c:902
REAL endian_convert_real(const REAL rnum, const INT vlength, const INT endianflag)
Swap order of a REAL number.
Definition: convert.c:137
INT * IA
integer array of row pointers, the size is m+1
Definition: fasp.h:160
INT * IA
integer array of row pointers, the size is ROW+1
Definition: fasp_block.h:70
INT ny
number of grids in y direction
Definition: fasp.h:310
void fasp_ivecind_read(const char *filename, ivector *b)
Read b from matrix disk file.
Definition: io.c:862
void fasp_dvec_print(INT n, dvector *u)
Print first n entries of a vector of REAL type.
Definition: io.c:1362
void fasp_dcsr_free(dCSRmat *A)
Free CSR sparse matrix data memory space.
Definition: sparse_csr.c:166