Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
Loading...
Searching...
No Matches
BlaIO.c
Go to the documentation of this file.
1
18#include "fasp.h"
19#include "fasp_functs.h"
20#include "hb_io.h"
21
22// Flags which indicates lengths of INT and REAL numbers
26/*---------------------------------*/
27/*-- Declare Private Functions --*/
28/*---------------------------------*/
29
30#include "BlaIOUtil.inl"
31
32/*---------------------------------*/
33/*-- Public Functions --*/
34/*---------------------------------*/
35
63void fasp_dcsrvec_read1(const char* filename, dCSRmat* A, dvector* b)
64{
65 int i, m, n, idata;
66 REAL ddata;
67
68 // Open input disk file
69 FILE* fp = fopen(filename, "r");
70
71 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
72
73 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
74
75 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
76
77 // Read CSR matrix
78 if (fscanf(fp, "%d %d", &m, &n) > 0) {
79 A->row = m;
80 A->col = n;
81 } else {
83 }
84
85 A->IA = (INT*)fasp_mem_calloc(m + 1, sizeof(INT));
86 for (i = 0; i <= m; ++i) {
87 if (fscanf(fp, "%d", &idata) > 0)
88 A->IA[i] = idata;
89 else {
91 }
92 }
93
94 INT nnz = A->IA[m] - A->IA[0];
95
96 A->nnz = nnz;
97 A->JA = (INT*)fasp_mem_calloc(nnz, sizeof(INT));
98 A->val = (REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
99
100 for (i = 0; i < nnz; ++i) {
101 if (fscanf(fp, "%d", &idata) > 0)
102 A->JA[i] = idata;
103 else {
104 fasp_chkerr(ERROR_WRONG_FILE, filename);
105 }
106 }
107
108 for (i = 0; i < nnz; ++i) {
109 if (fscanf(fp, "%lf", &ddata) > 0)
110 A->val[i] = ddata;
111 else {
112 fasp_chkerr(ERROR_WRONG_FILE, filename);
113 }
114 }
115
116 // Read RHS vector
117 if (fscanf(fp, "%d", &m) > 0) b->row = m;
118
119 b->val = (REAL*)fasp_mem_calloc(m, sizeof(REAL));
120
121 for (i = 0; i < m; ++i) {
122 if (fscanf(fp, "%lf", &ddata) > 0)
123 b->val[i] = ddata;
124 else {
125 fasp_chkerr(ERROR_WRONG_FILE, filename);
126 }
127 }
128
129 fclose(fp);
130}
131
164void fasp_dcsrvec_read2(const char* filemat, const char* filerhs, dCSRmat* A,
165 dvector* b)
166{
167 int i, n, tempi;
168
169 /* read the matrix from file */
170 FILE* fp = fopen(filemat, "r");
171
172 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filemat);
173
174 printf("%s: reading file %s ...\n", __FUNCTION__, filemat);
175
176 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
177
178 if (fscanf(fp, "%d\n", &n) > 0) {
179 A->row = n;
180 A->col = n;
181 A->IA = (INT*)fasp_mem_calloc(n + 1, sizeof(INT));
182 } else {
184 }
185
186 for (i = 0; i <= n; ++i) {
187 if (fscanf(fp, "%d\n", &tempi) > 0)
188 A->IA[i] = tempi - 1;
189 else {
191 }
192 }
193
194 INT nz = A->IA[n];
195 A->nnz = nz;
196 A->JA = (INT*)fasp_mem_calloc(nz, sizeof(INT));
197 A->val = (REAL*)fasp_mem_calloc(nz, sizeof(REAL));
198
199 for (i = 0; i < nz; ++i) {
200 if (fscanf(fp, "%d\n", &tempi) > 0)
201 A->JA[i] = tempi - 1;
202 else {
204 }
205 }
206
207 for (i = 0; i < nz; ++i) {
208 if (fscanf(fp, "%le\n", &(A->val[i])) <= 0) {
210 }
211 }
212
213 fclose(fp);
214
215 /* Read the rhs from file */
216 b->row = n;
217 b->val = (REAL*)fasp_mem_calloc(n, sizeof(REAL));
218
219 fp = fopen(filerhs, "r");
220
221 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filerhs);
222
223 printf("%s: reading file %s ...\n", __FUNCTION__, filerhs);
224
225 if (fscanf(fp, "%d\n", &n) < 0) fasp_chkerr(ERROR_WRONG_FILE, filerhs);
226
227 if (n != b->row) {
228 printf("### WARNING: rhs size = %d, matrix size = %d!\n", n, b->row);
229 fasp_chkerr(ERROR_MAT_SIZE, filemat);
230 }
231
232 for (i = 0; i < n; ++i) {
233 if (fscanf(fp, "%le\n", &(b->val[i])) <= 0) {
235 }
236 }
237
238 fclose(fp);
239}
240
252void fasp_dcsr_read(const char* filename, dCSRmat* A)
253{
254 int i, m, idata;
255 REAL ddata;
256
257 // Open input disk file
258 FILE* fp = fopen(filename, "r");
259
260 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
261
262 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
263
264 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
265
266 // Read CSR matrix
267 if (fscanf(fp, "%d", &m) > 0)
268 A->row = A->col = m;
269 else {
270 fasp_chkerr(ERROR_WRONG_FILE, filename);
271 }
272
273 A->IA = (INT*)fasp_mem_calloc(m + 1, sizeof(INT));
274 for (i = 0; i <= m; ++i) {
275 if (fscanf(fp, "%d", &idata) > 0)
276 A->IA[i] = idata;
277 else {
278 fasp_chkerr(ERROR_WRONG_FILE, filename);
279 }
280 }
281
282 // If IA starts from 1, shift by -1
283 if (A->IA[0] == 1)
284 for (i = 0; i <= m; ++i) A->IA[i]--;
285
286 INT nnz = A->IA[m] - A->IA[0];
287
288 A->nnz = nnz;
289 A->JA = (INT*)fasp_mem_calloc(nnz, sizeof(INT));
290 A->val = (REAL*)fasp_mem_calloc(nnz, sizeof(REAL));
291
292 for (i = 0; i < nnz; ++i) {
293 if (fscanf(fp, "%d", &idata) > 0)
294 A->JA[i] = idata;
295 else {
296 fasp_chkerr(ERROR_WRONG_FILE, filename);
297 }
298 }
299
300 // If JA starts from 1, shift by -1
301 if (A->JA[0] == 1)
302 for (i = 0; i < nnz; ++i) A->JA[i]--;
303
304 for (i = 0; i < nnz; ++i) {
305 if (fscanf(fp, "%lf", &ddata) > 0)
306 A->val[i] = ddata;
307 else {
308 fasp_chkerr(ERROR_WRONG_FILE, filename);
309 }
310 }
311
312 fclose(fp);
313}
314
332void fasp_dcoo_read(const char* filename, dCSRmat* A)
333{
334 int i, j, k, m, n, nnz;
335 REAL value;
336
337 FILE* fp = fopen(filename, "r");
338
339 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
340
341 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
342
343 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
344
345 if (fscanf(fp, "%d %d %d", &m, &n, &nnz) <= 0) {
346 fasp_chkerr(ERROR_WRONG_FILE, filename);
347 }
348
349 dCOOmat Atmp = fasp_dcoo_create(m, n, nnz);
350
351 for (k = 0; k < nnz; k++) {
352 if (fscanf(fp, "%d %d %le", &i, &j, &value) != EOF) {
353 Atmp.rowind[k] = i;
354 Atmp.colind[k] = j;
355 Atmp.val[k] = value;
356 } else {
357 fasp_chkerr(ERROR_WRONG_FILE, filename);
358 }
359 }
360
361 fclose(fp);
362
363 fasp_format_dcoo_dcsr(&Atmp, A);
364 fasp_dcoo_free(&Atmp);
365}
366
384void fasp_dcoo_read1(const char* filename, dCSRmat* A)
385{
386 int i, j, k, m, n, nnz;
387 REAL value;
388
389 FILE* fp = fopen(filename, "r");
390
391 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
392
393 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
394
395 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
396
397 if (fscanf(fp, "%d %d %d", &m, &n, &nnz) <= 0) {
398 fasp_chkerr(ERROR_WRONG_FILE, filename);
399 }
400
401 dCOOmat Atmp = fasp_dcoo_create(m, n, nnz);
402
403 for (k = 0; k < nnz; k++) {
404 if (fscanf(fp, "%d %d %le", &i, &j, &value) != EOF) {
405 Atmp.rowind[k] = i - 1;
406 Atmp.colind[k] = j - 1;
407 Atmp.val[k] = value;
408 } else {
409 fasp_chkerr(ERROR_WRONG_FILE, filename);
410 }
411 }
412
413 fclose(fp);
414
415 fasp_format_dcoo_dcsr(&Atmp, A);
416 fasp_dcoo_free(&Atmp);
417}
418
437void fasp_dcoovec_bin_read(const char* fni, const char* fnj, const char* fna,
438 const char* fnb, dCSRmat* A, dvector* b)
439{
440 size_t n, type, nnz, i;
441 FILE* fp;
442
443 fp = fopen(fnb, "rb");
444 if (fp == NULL) {
446 }
447 printf("%s: reading file %s ...\n", __FUNCTION__, fnb);
448 fread(&n, sizeof(size_t), 1, fp);
449 b->row = n;
450 b->val = (double*)fasp_mem_calloc(n, sizeof(double));
451 fread(b->val, sizeof(double), n, fp);
452 fclose(fp);
453
454 fp = fopen(fni, "rb");
455 if (fp == NULL) {
457 }
458 printf("%s: reading file %s ...\n", __FUNCTION__, fni);
459 fread(&type, sizeof(size_t), 1, fp);
460 fread(&nnz, sizeof(size_t), 1, fp);
461 dCOOmat Atmp = fasp_dcoo_create(n, n, nnz);
462 Atmp.rowind = (int*)fasp_mem_calloc(nnz, sizeof(int));
463 fread(Atmp.rowind, sizeof(int), nnz, fp);
464 for (i = 0; i < nnz; i++) Atmp.rowind[i] = Atmp.rowind[i] - 1;
465 fclose(fp);
466
467 fp = fopen(fnj, "rb");
468 if (fp == NULL) {
470 }
471 printf("%s: reading file %s ...\n", __FUNCTION__, fnj);
472 fread(&type, sizeof(size_t), 1, fp);
473 fread(&nnz, sizeof(size_t), 1, fp);
474 Atmp.colind = (int*)fasp_mem_calloc(nnz, sizeof(int));
475 fread(Atmp.colind, sizeof(int), nnz, fp);
476 for (i = 0; i < nnz; i++) Atmp.colind[i] = Atmp.colind[i] - 1;
477 fclose(fp);
478
479 fp = fopen(fna, "rb");
480 if (fp == NULL) {
482 }
483 printf("%s: reading file %s ...\n", __FUNCTION__, fna);
484 fread(&type, sizeof(size_t), 1, fp);
485 fread(&nnz, sizeof(size_t), 1, fp);
486 Atmp.val = (double*)fasp_mem_calloc(nnz, sizeof(double));
487 fread(Atmp.val, sizeof(double), nnz, fp);
488 fclose(fp);
489
490 fasp_format_dcoo_dcsr(&Atmp, A);
491 fasp_dcoo_free(&Atmp);
492}
493
514void fasp_dcoo_shift_read(const char* filename, dCSRmat* A)
515{
516 int i, j, k, m, n, nnz;
517 REAL value;
518
519 FILE* fp = fopen(filename, "r");
520
521 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
522
523 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
524
525 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
526
527 if (fscanf(fp, "%d %d %d", &m, &n, &nnz) <= 0) {
528 fasp_chkerr(ERROR_WRONG_FILE, filename);
529 }
530
531 dCOOmat Atmp = fasp_dcoo_create(m, n, nnz);
532
533 for (k = 0; k < nnz; k++) {
534 if (fscanf(fp, "%d %d %le", &i, &j, &value) != EOF) {
535 Atmp.rowind[k] = i - 1;
536 Atmp.colind[k] = j - 1;
537 Atmp.val[k] = value;
538 } else {
539 fasp_chkerr(ERROR_WRONG_FILE, filename);
540 }
541 }
542
543 fclose(fp);
544
545 fasp_format_dcoo_dcsr(&Atmp, A);
546 fasp_dcoo_free(&Atmp);
547}
548
567void fasp_dmtx_read(const char* filename, dCSRmat* A)
568{
569 int i, j, m, n, nnz;
570 INT innz; // index of nonzeros
571 REAL value;
572
573 FILE* fp = fopen(filename, "r");
574
575 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
576
577 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
578
579 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
580
581 if (fscanf(fp, "%d %d %d", &m, &n, &nnz) <= 0) {
582 fasp_chkerr(ERROR_WRONG_FILE, filename);
583 }
584
585 dCOOmat Atmp = fasp_dcoo_create(m, n, nnz);
586
587 innz = 0;
588
589 while (innz < nnz) {
590 if (fscanf(fp, "%d %d %le", &i, &j, &value) != EOF) {
591 Atmp.rowind[innz] = i - 1;
592 Atmp.colind[innz] = j - 1;
593 Atmp.val[innz] = value;
594 innz = innz + 1;
595 } else {
596 fasp_chkerr(ERROR_WRONG_FILE, filename);
597 }
598 }
599
600 fclose(fp);
601
602 fasp_format_dcoo_dcsr(&Atmp, A);
603 fasp_dcoo_free(&Atmp);
604}
605
624void fasp_dmtxsym_read(const char* filename, dCSRmat* A)
625{
626 int i, j, m, n, nnz;
627 int innz; // index of nonzeros
628 REAL value;
629
630 FILE* fp = fopen(filename, "r");
631
632 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
633
634 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
635
636 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
637
638 if (fscanf(fp, "%d %d %d", &m, &n, &nnz) <= 0) {
639 fasp_chkerr(ERROR_WRONG_FILE, filename);
640 }
641
642 nnz = 2 * (nnz - m) + m; // adjust for sym problem
643 dCOOmat Atmp = fasp_dcoo_create(m, n, nnz);
644
645 innz = 0;
646
647 while (innz < nnz) {
648 if (fscanf(fp, "%d %d %le", &i, &j, &value) != EOF) {
649
650 if (i == j) {
651 Atmp.rowind[innz] = i - 1;
652 Atmp.colind[innz] = j - 1;
653 Atmp.val[innz] = value;
654 innz = innz + 1;
655 } else {
656 Atmp.rowind[innz] = i - 1;
657 Atmp.rowind[innz + 1] = j - 1;
658 Atmp.colind[innz] = j - 1;
659 Atmp.colind[innz + 1] = i - 1;
660 Atmp.val[innz] = value;
661 Atmp.val[innz + 1] = value;
662 innz = innz + 2;
663 }
664 } else {
665 fasp_chkerr(ERROR_WRONG_FILE, filename);
666 }
667 }
668
669 fclose(fp);
670
671 fasp_format_dcoo_dcsr(&Atmp, A);
672 fasp_dcoo_free(&Atmp);
673}
674
699void fasp_dstr_read(const char* filename, dSTRmat* A)
700{
701 int nx, ny, nz, nxy, ngrid, nband, nc, offset;
702 int i, k, n;
703 REAL value;
704
705 FILE* fp = fopen(filename, "r");
706
707 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
708
709 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
710
711 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
712
713 // read dimension of the problem
714 if (fscanf(fp, "%d %d %d", &nx, &ny, &nz) > 0) {
715 A->nx = nx;
716 A->ny = ny;
717 A->nz = nz;
718 } else {
719 fasp_chkerr(ERROR_WRONG_FILE, filename);
720 }
721
722 nxy = nx * ny;
723 ngrid = nxy * nz;
724 A->nxy = nxy;
725 A->ngrid = ngrid;
726
727 // read number of components
728 if (fscanf(fp, "%d", &nc) > 0)
729 A->nc = nc;
730 else {
731 fasp_chkerr(ERROR_WRONG_FILE, filename);
732 }
733
734 // read number of bands
735 if (fscanf(fp, "%d", &nband) > 0)
736 A->nband = nband;
737 else {
738 fasp_chkerr(ERROR_WRONG_FILE, filename);
739 }
740
741 A->offsets = (INT*)fasp_mem_calloc(nband, sizeof(INT));
742
743 // read diagonal
744 if (fscanf(fp, "%d", &n) > 0) {
745 A->diag = (REAL*)fasp_mem_calloc(n, sizeof(REAL));
746 } else {
747 fasp_chkerr(ERROR_WRONG_FILE, filename);
748 }
749
750 for (i = 0; i < n; ++i) {
751 if (fscanf(fp, "%le", &value) > 0)
752 A->diag[i] = value;
753 else {
754 fasp_chkerr(ERROR_WRONG_FILE, filename);
755 }
756 }
757
758 // read offdiags
759 k = nband;
760 A->offdiag = (REAL**)fasp_mem_calloc(nband, sizeof(REAL*));
761 while (k--) {
762 // read number band k
763 if (fscanf(fp, "%d %d", &offset, &n) > 0) {
764 A->offsets[nband - k - 1] = offset;
765 } else {
766 fasp_chkerr(ERROR_WRONG_FILE, filename);
767 }
768
769 A->offdiag[nband - k - 1] = (REAL*)fasp_mem_calloc(n, sizeof(REAL));
770 for (i = 0; i < n; ++i) {
771 if (fscanf(fp, "%le", &value) > 0) {
772 A->offdiag[nband - k - 1][i] = value;
773 } else {
774 fasp_chkerr(ERROR_WRONG_FILE, filename);
775 }
776 }
777 }
778
779 fclose(fp);
780}
781
807void fasp_dbsr_read(const char* filename, dBSRmat* A)
808{
809 int ROW, COL, NNZ, nb, storage_manner;
810 int i, n;
811 int index;
812 REAL value;
813 size_t status;
814
815 FILE* fp = fopen(filename, "r");
816
817 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
818
819 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
820
821 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
822
823 status = fscanf(fp, "%d %d %d", &ROW, &COL, &NNZ); // dimensions of the problem
824 fasp_chkerr(status, filename);
825 A->ROW = ROW;
826 A->COL = COL;
827 A->NNZ = NNZ;
828
829 status = fscanf(fp, "%d", &nb); // read the size of each block
830 fasp_chkerr(status, filename);
831 A->nb = nb;
832
833 status = fscanf(fp, "%d", &storage_manner); // read the storage_manner
834 fasp_chkerr(status, filename);
835 A->storage_manner = storage_manner;
836
837 // allocate memory space
838 fasp_dbsr_alloc(ROW, COL, NNZ, nb, storage_manner, A);
839
840 // read IA
841 status = fscanf(fp, "%d", &n);
842 fasp_chkerr(status, filename);
843 for (i = 0; i < n; ++i) {
844 status = fscanf(fp, "%d", &index);
845 fasp_chkerr(status, filename);
846 A->IA[i] = index;
847 }
848
849 // read JA
850 status = fscanf(fp, "%d", &n);
851 fasp_chkerr(status, filename);
852 for (i = 0; i < n; ++i) {
853 status = fscanf(fp, "%d", &index);
854 fasp_chkerr(status, filename);
855 A->JA[i] = index;
856 }
857
858 // read val
859 status = fscanf(fp, "%d", &n);
860 fasp_chkerr(status, filename);
861 for (i = 0; i < n; ++i) {
862 status = fscanf(fp, "%le", &value);
863 fasp_chkerr(status, filename);
864 A->val[i] = value;
865 }
866
867 fclose(fp);
868}
869
887void fasp_dvecind_read(const char* filename, dvector* b)
888{
889 int i, n, index;
890 REAL value;
891 size_t status;
892
893 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
894
895 FILE* fp = fopen(filename, "r");
896
897 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
898
899 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
900
901 status = fscanf(fp, "%d", &n);
902 fasp_dvec_alloc(n, b);
903
904 for (i = 0; i < n; ++i) {
905
906 status = fscanf(fp, "%d %le", &index, &value);
907
908 if (value > BIGREAL || index >= n) {
910 fclose(fp);
911
912 printf("### ERROR: Wrong index = %d or value = %lf\n", index, value);
913 fasp_chkerr(ERROR_INPUT_PAR, __FUNCTION__);
914 }
915
916 b->val[index] = value;
917 }
918
919 fclose(fp);
920 fasp_chkerr(status, filename);
921}
922
938void fasp_dvec_read(const char* filename, dvector* b)
939{
940 int i, n;
941 REAL value;
942 size_t status;
943
944 FILE* fp = fopen(filename, "r");
945
946 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
947
948 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
949
950 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
951
952 status = fscanf(fp, "%d", &n);
953
954 fasp_dvec_alloc(n, b);
955
956 for (i = 0; i < n; ++i) {
957
958 status = fscanf(fp, "%le", &value);
959 b->val[i] = value;
960
961 if (value > BIGREAL) {
963 fclose(fp);
964
965 printf("### ERROR: Wrong value = %lf!\n", value);
966 fasp_chkerr(ERROR_INPUT_PAR, __FUNCTION__);
967 }
968 }
969
970 fclose(fp);
971 fasp_chkerr(status, filename);
972}
973
989void fasp_ivecind_read(const char* filename, ivector* b)
990{
991 int i, n, index, value;
992 size_t status;
993
994 FILE* fp = fopen(filename, "r");
995
996 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
997
998 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
999
1000 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
1001
1002 status = fscanf(fp, "%d", &n);
1003 fasp_ivec_alloc(n, b);
1004
1005 for (i = 0; i < n; ++i) {
1006 status = fscanf(fp, "%d %d", &index, &value);
1007 b->val[index] = value;
1008 }
1009
1010 fclose(fp);
1011 fasp_chkerr(status, filename);
1012}
1013
1029void fasp_ivec_read(const char* filename, ivector* b)
1030{
1031 int i, n, value;
1032 size_t status;
1033
1034 FILE* fp = fopen(filename, "r");
1035
1036 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1037
1038 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
1039
1040 skip_comments(fp); // skip the comments in the beginning --zcs 06/30/2020
1041
1042 status = fscanf(fp, "%d", &n);
1043 fasp_ivec_alloc(n, b);
1044
1045 for (i = 0; i < n; ++i) {
1046 status = fscanf(fp, "%d", &value);
1047 b->val[i] = value;
1048 }
1049
1050 fclose(fp);
1051 fasp_chkerr(status, filename);
1052}
1053
1079void fasp_dcsrvec_write1(const char* filename, dCSRmat* A, dvector* b)
1080{
1081 INT m = A->row, n = A->col, nnz = A->nnz;
1082 INT i;
1083
1084 FILE* fp = fopen(filename, "w");
1085
1086 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1087
1088 /* write the matrix to file */
1089 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
1090
1091 fprintf(fp, "%d %d\n", m, n);
1092 for (i = 0; i < m + 1; ++i) {
1093 fprintf(fp, "%d\n", A->IA[i]);
1094 }
1095 for (i = 0; i < nnz; ++i) {
1096 fprintf(fp, "%d\n", A->JA[i]);
1097 }
1098 for (i = 0; i < nnz; ++i) {
1099 fprintf(fp, "%le\n", A->val[i]);
1100 }
1101
1102 m = b->row;
1103
1104 /* write the rhs to file */
1105 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1106
1107 fprintf(fp, "%d\n", m);
1108
1109 for (i = 0; i < m; ++i) fprintf(fp, "%le\n", b->val[i]);
1110
1111 fclose(fp);
1112}
1113
1145void fasp_dcsrvec_write2(const char* filemat, const char* filerhs, dCSRmat* A,
1146 dvector* b)
1147{
1148 INT m = A->row, nnz = A->nnz;
1149 INT i;
1150
1151 FILE* fp = fopen(filemat, "w");
1152
1153 /* write the matrix to file */
1154 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filemat);
1155
1156 printf("%s: writing to file %s ...\n", __FUNCTION__, filemat);
1157
1158 fprintf(fp, "%d\n", m);
1159 for (i = 0; i < m + 1; ++i) {
1160 fprintf(fp, "%d\n", A->IA[i] + 1);
1161 }
1162 for (i = 0; i < nnz; ++i) {
1163 fprintf(fp, "%d\n", A->JA[i] + 1);
1164 }
1165 for (i = 0; i < nnz; ++i) {
1166 fprintf(fp, "%le\n", A->val[i]);
1167 }
1168
1169 fclose(fp);
1170
1171 m = b->row;
1172
1173 fp = fopen(filerhs, "w");
1174
1175 /* write the rhs to file */
1176 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filerhs);
1177
1178 printf("%s: writing to file %s ...\n", __FUNCTION__, filerhs);
1179
1180 fprintf(fp, "%d\n", m);
1181
1182 for (i = 0; i < m; ++i) fprintf(fp, "%le\n", b->val[i]);
1183
1184 fclose(fp);
1185}
1186
1207void fasp_dcoo_write(const char* filename, dCSRmat* A)
1208{
1209 const INT m = A->row, n = A->col;
1210 INT i, j;
1211
1212 FILE* fp = fopen(filename, "w");
1213
1214 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1215
1216 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1217
1218 fprintf(fp, "%d %d %d\n", m, n, A->nnz);
1219 for (i = 0; i < m; ++i) {
1220 for (j = A->IA[i]; j < A->IA[i + 1]; j++)
1221 fprintf(fp, "%d %d %0.15e\n", i, A->JA[j], A->val[j]);
1222 }
1223
1224 fclose(fp);
1225}
1226
1241void fasp_dstr_write(const char* filename, dSTRmat* A)
1242{
1243 const INT nx = A->nx, ny = A->ny, nz = A->nz;
1244 const INT ngrid = A->ngrid, nband = A->nband, nc = A->nc;
1245
1246 INT* offsets = A->offsets;
1247
1248 INT i, k, n;
1249
1250 FILE* fp = fopen(filename, "w");
1251
1252 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1253
1254 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1255
1256 fprintf(fp, "%d %d %d\n", nx, ny, nz); // write dimension of the problem
1257
1258 fprintf(fp, "%d\n", nc); // read number of components
1259
1260 fprintf(fp, "%d\n", nband); // write number of bands
1261
1262 // write diagonal
1263 n = ngrid * nc * nc; // number of nonzeros in each band
1264 fprintf(fp, "%d\n", n); // number of diagonal entries
1265 for (i = 0; i < n; ++i) fprintf(fp, "%le\n", A->diag[i]);
1266
1267 // write offdiags
1268 k = nband;
1269 while (k--) {
1270 INT offset = offsets[nband - k - 1];
1271 n = (ngrid - ABS(offset)) * nc * nc; // number of nonzeros in each band
1272 fprintf(fp, "%d %d\n", offset, n); // read number band k
1273 for (i = 0; i < n; ++i) {
1274 fprintf(fp, "%le\n", A->offdiag[nband - k - 1][i]);
1275 }
1276 }
1277
1278 fclose(fp);
1279}
1280
1292void fasp_dbsr_print(const char* filename, dBSRmat* A)
1293{
1294 const INT ROW = A->ROW;
1295 const INT nb = A->nb;
1296 const INT nb2 = nb * nb;
1297
1298 INT* ia = A->IA;
1299 INT* ja = A->JA;
1300 REAL* val = A->val;
1301
1302 INT i, j, k, ind;
1303
1304 FILE* fp = fopen(filename, "w");
1305
1306 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1307
1308 printf("%s: printing to file %s ...\n", __FUNCTION__, filename);
1309
1310 for (i = 0; i < ROW; i++) {
1311 for (k = ia[i]; k < ia[i + 1]; k++) {
1312 j = ja[k];
1313 fprintf(fp, "A[%d,%d]=\n", i, j);
1314 for (ind = 0; ind < nb2; ind++) {
1315 fprintf(fp, "%+.15E ", val[k * nb2 + ind]);
1316 }
1317 fprintf(fp, "\n");
1318 }
1319 }
1320}
1321
1336void fasp_dbsr_write(const char* filename, dBSRmat* A)
1337{
1338 const INT ROW = A->ROW, COL = A->COL, NNZ = A->NNZ;
1339 const INT nb = A->nb, storage_manner = A->storage_manner;
1340
1341 INT* ia = A->IA;
1342 INT* ja = A->JA;
1343 REAL* val = A->val;
1344
1345 INT i, n;
1346
1347 FILE* fp = fopen(filename, "w");
1348
1349 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1350
1351 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1352
1353 fprintf(fp, "%d %d %d\n", ROW, COL, NNZ); // write dimension of the block matrix
1354
1355 fprintf(fp, "%d\n", nb); // write the size of each block
1356
1357 fprintf(fp, "%d\n", storage_manner); // write storage manner of each block
1358
1359 // write A->IA
1360 n = ROW + 1; // length of A->IA
1361 fprintf(fp, "%d\n", n); // length of A->IA
1362 for (i = 0; i < n; ++i) fprintf(fp, "%d\n", ia[i]);
1363
1364 // write A->JA
1365 n = NNZ; // length of A->JA
1366 fprintf(fp, "%d\n", n); // length of A->JA
1367 for (i = 0; i < n; ++i) fprintf(fp, "%d\n", ja[i]);
1368
1369 // write A->val
1370 n = NNZ * nb * nb; // length of A->val
1371 fprintf(fp, "%d\n", n); // length of A->val
1372 for (i = 0; i < n; ++i) fprintf(fp, "%le\n", val[i]);
1373
1374 fclose(fp);
1375}
1376
1388void fasp_dvec_write(const char* filename, dvector* vec)
1389{
1390 INT m = vec->row, i;
1391
1392 FILE* fp = fopen(filename, "w");
1393
1394 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1395
1396 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1397
1398 fprintf(fp, "%d\n", m);
1399
1400 for (i = 0; i < m; ++i) fprintf(fp, "%0.15e\n", vec->val[i]);
1401
1402 fclose(fp);
1403}
1404
1420void fasp_dvecind_write(const char* filename, dvector* vec)
1421{
1422 INT m = vec->row, i;
1423
1424 FILE* fp = fopen(filename, "w");
1425
1426 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1427
1428 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1429
1430 fprintf(fp, "%d\n", m);
1431
1432 for (i = 0; i < m; ++i) fprintf(fp, "%d %le\n", i, vec->val[i]);
1433
1434 fclose(fp);
1435}
1436
1452void fasp_ivec_write(const char* filename, ivector* vec)
1453{
1454 INT m = vec->row, i;
1455
1456 FILE* fp = fopen(filename, "w");
1457
1458 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1459
1460 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1461
1462 // write number of nonzeros
1463 fprintf(fp, "%d\n", m);
1464
1465 // write index and value each line
1466 for (i = 0; i < m; ++i) fprintf(fp, "%d %d\n", i, vec->val[i] + 1);
1467
1468 fclose(fp);
1469}
1470
1482void fasp_dvec_print(const INT n, dvector* u)
1483{
1484 INT i;
1485 INT NumPrint = n;
1486
1487 if (n <= 0) NumPrint = u->row; // print all
1488
1489 for (i = 0; i < NumPrint; ++i) printf("vec_%d = %15.10E\n", i, u->val[i]);
1490}
1491
1503void fasp_ivec_print(const INT n, ivector* u)
1504{
1505 INT i;
1506 INT NumPrint = n;
1507
1508 if (n <= 0) NumPrint = u->row; // print all
1509
1510 for (i = 0; i < NumPrint; ++i) printf("vec_%d = %d\n", i, u->val[i]);
1511}
1512
1524{
1525 const INT m = A->row, n = A->col;
1526 INT i, j;
1527
1528 printf("nrow = %d, ncol = %d, nnz = %d\n", m, n, A->nnz);
1529 for (i = 0; i < m; ++i) {
1530 for (j = A->IA[i]; j < A->IA[i + 1]; j++)
1531 printf("A_(%d,%d) = %+.15E\n", i, A->JA[j], A->val[j]);
1532 }
1533}
1534
1546{
1547 INT k;
1548
1549 printf("nrow = %d, ncol = %d, nnz = %d\n", A->row, A->col, A->nnz);
1550 for (k = 0; k < A->nnz; k++) {
1551 printf("A_(%d,%d) = %+.15E\n", A->rowind[k], A->colind[k], A->val[k]);
1552 }
1553}
1554
1568void fasp_dbsr_write_coo(const char* filename, const dBSRmat* A)
1569{
1570
1571 INT i, j, k, l;
1572 INT nb, nb2;
1573 nb = A->nb;
1574 nb2 = nb * nb;
1575
1576 FILE* fp = fopen(filename, "w");
1577
1578 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1579
1580#if DEBUG_MODE > PRINT_MIN
1581 printf("### DEBUG: nrow = %d, ncol = %d, nnz = %d, nb = %d\n", A->ROW, A->COL,
1582 A->NNZ, A->nb);
1583 printf("### DEBUG: storage_manner = %d\n", A->storage_manner);
1584#endif
1585
1586 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1587
1588 // write dimension of the block matrix
1589 fprintf(fp, "%% dimension of the block matrix and nonzeros %d %d %d\n", A->ROW,
1590 A->COL, A->NNZ);
1591 // write the size of each block
1592 fprintf(fp, "%% the size of each block %d\n", A->nb);
1593 // write storage manner of each block
1594 fprintf(fp, "%% storage manner of each block %d\n", A->storage_manner);
1595
1596 for (i = 0; i < A->ROW; i++) {
1597 for (j = A->IA[i]; j < A->IA[i + 1]; j++) {
1598 for (k = 0; k < A->nb; k++) {
1599 for (l = 0; l < A->nb; l++) {
1600 fprintf(fp, "%d %d %+.15E\n", i * nb + k + 1, A->JA[j] * nb + l + 1,
1601 A->val[j * nb2 + k * nb + l]);
1602 }
1603 }
1604 }
1605 }
1606
1607 fclose(fp);
1608}
1609
1623void fasp_dcsr_write_coo(const char* filename, const dCSRmat* A)
1624{
1625
1626 INT i, j;
1627
1628#if DEBUG_MODE > PRINT_MIN
1629 printf("nrow = %d, ncol = %d, nnz = %d\n", A->row, A->col, A->nnz);
1630#endif
1631
1632 FILE* fp = fopen(filename, "w");
1633
1634 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1635
1636 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1637
1638 // write dimension of the matrix
1639 fprintf(fp, "%% dimension of the matrix and nonzeros %d %d %d\n", A->row, A->col,
1640 A->nnz);
1641
1642 for (i = 0; i < A->row; i++) {
1643 for (j = A->IA[i]; j < A->IA[i + 1]; j++) {
1644 fprintf(fp, "%d %d %+.15E\n", i + 1, A->JA[j] + 1, A->val[j]);
1645 }
1646 }
1647
1648 fclose(fp);
1649}
1650
1664void fasp_dcsr_write_mtx(const char* filename, const dCSRmat* A)
1665{
1666 INT i, j;
1667
1668#if DEBUG_MODE > PRINT_MIN
1669 printf("nrow = %d, ncol = %d, nnz = %d\n", A->row, A->col, A->nnz);
1670#endif
1671
1672 FILE* fp = fopen(filename, "w");
1673
1674 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1675
1676 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1677
1678 // write dimension of the matrix
1679 fprintf(fp, "%% MatrixMarket matrix coordinate general\n");
1680 fprintf(fp, "%d %d %d\n", A->row, A->col, A->nnz);
1681
1682 for (i = 0; i < A->row; i++) {
1683 for (j = A->IA[i]; j < A->IA[i + 1]; j++) {
1684 fprintf(fp, "%d %d %+.15E\n", i + 1, A->JA[j] + 1, A->val[j]);
1685 }
1686 }
1687
1688 fclose(fp);
1689}
1690
1702{
1703 // TODO: To be added later! --Chensong
1704}
1705
1735void fasp_matrix_read(const char* filename, void* A)
1736{
1737
1738 int index, flag;
1739 SHORT EndianFlag;
1740 size_t status;
1741
1742 FILE* fp = fopen(filename, "rb");
1743
1744 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1745
1746 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
1747
1748 status = fread(&index, sizeof(INT), 1, fp);
1749 fasp_chkerr(status, filename);
1750
1751 // matrix stored in ASCII format
1752 if (index == 808464432) {
1753
1754 fclose(fp);
1755 fp = fopen(filename, "r"); // reopen file of reading file in ASCII
1756
1757 status = fscanf(fp, "%d\n", &flag); // jump over the first line
1758 fasp_chkerr(status, __FUNCTION__);
1759
1760 status = fscanf(fp, "%d\n", &flag); // reading the format information
1761 fasp_chkerr(status, __FUNCTION__);
1762
1763 flag = (INT)flag / 100;
1764
1765 switch (flag) {
1766 case 0:
1767 fasp_dcsr_read_s(fp, (dCSRmat*)A);
1768 break;
1769 case 1:
1770 fasp_dcoo_read_s(fp, (dCSRmat*)A);
1771 break;
1772 case 2:
1773 fasp_dbsr_read_s(fp, (dBSRmat*)A);
1774 break;
1775 case 3:
1776 fasp_dstr_read_s(fp, (dSTRmat*)A);
1777 break;
1778 case 4:
1779 fasp_dcoo_read_s(fp, (dCSRmat*)A);
1780 break;
1781 case 5:
1782 fasp_dmtx_read_s(fp, (dCSRmat*)A);
1783 break;
1784 case 6:
1785 fasp_dmtxsym_read_s(fp, (dCSRmat*)A);
1786 break;
1787 default:
1788 printf("### ERROR: Unknown flag %d in %s!\n", flag, filename);
1789 fasp_chkerr(ERROR_WRONG_FILE, __FUNCTION__);
1790 }
1791
1792 fclose(fp);
1793 return;
1794 }
1795
1796 // matrix stored in binary format
1797
1798 // test Endian consistence of machine and file
1799 EndianFlag = index;
1800
1801 status = fread(&index, sizeof(INT), 1, fp);
1802 fasp_chkerr(status, filename);
1803
1804 index = endian_convert_int(index, sizeof(INT), EndianFlag);
1805 flag = (INT)index / 100;
1806 ilength = (INT)(index - flag * 100) / 10;
1807 dlength = index % 10;
1808
1809 switch (flag) {
1810 case 1:
1811 fasp_dcsr_read_b(fp, (dCSRmat*)A, EndianFlag);
1812 break;
1813 case 2:
1814 fasp_dbsr_read_b(fp, (dBSRmat*)A, EndianFlag);
1815 break;
1816 case 3:
1817 fasp_dstr_read_b(fp, (dSTRmat*)A, EndianFlag);
1818 break;
1819 case 4:
1820 fasp_dcoo_read_b(fp, (dCSRmat*)A, EndianFlag);
1821 break;
1822 case 5:
1823 fasp_dmtx_read_b(fp, (dCSRmat*)A, EndianFlag);
1824 break;
1825 case 6:
1826 fasp_dmtxsym_read_b(fp, (dCSRmat*)A, EndianFlag);
1827 break;
1828 default:
1829 printf("### ERROR: Unknown flag %d in %s!\n", flag, filename);
1830 fasp_chkerr(ERROR_WRONG_FILE, __FUNCTION__);
1831 }
1832
1833 fclose(fp);
1834}
1835
1849void fasp_matrix_read_bin(const char* filename, void* A)
1850{
1851 int index, flag;
1852 SHORT EndianFlag = 1;
1853 size_t status;
1854
1855 FILE* fp = fopen(filename, "rb");
1856
1857 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1858
1859 printf("%s: reading file %s ...\n", __FUNCTION__, filename);
1860
1861 status = fread(&index, sizeof(INT), 1, fp);
1862 fasp_chkerr(status, filename);
1863
1864 index = endian_convert_int(index, sizeof(INT), EndianFlag);
1865
1866 flag = (INT)index / 100;
1867 ilength = (int)(index - flag * 100) / 10;
1868 dlength = index % 10;
1869
1870 switch (flag) {
1871 case 1:
1872 fasp_dcoo_read_b(fp, (dCSRmat*)A, EndianFlag);
1873 break;
1874 case 2:
1875 fasp_dbsr_read_b(fp, (dBSRmat*)A, EndianFlag);
1876 break;
1877 case 3:
1878 fasp_dstr_read_b(fp, (dSTRmat*)A, EndianFlag);
1879 break;
1880 case 4:
1881 fasp_dcsr_read_b(fp, (dCSRmat*)A, EndianFlag);
1882 break;
1883 case 5:
1884 fasp_dmtx_read_b(fp, (dCSRmat*)A, EndianFlag);
1885 break;
1886 case 6:
1887 fasp_dmtxsym_read_b(fp, (dCSRmat*)A, EndianFlag);
1888 break;
1889 default:
1890 printf("### ERROR: Unknown flag %d in %s!\n", flag, filename);
1891 fasp_chkerr(ERROR_WRONG_FILE, __FUNCTION__);
1892 }
1893
1894 fclose(fp);
1895}
1896
1921void fasp_matrix_write(const char* filename, void* A, const INT flag)
1922{
1923 INT fileflag, matrixflag;
1924 FILE* fp;
1925
1926 matrixflag = flag % 100;
1927 fileflag = (INT)flag / 100;
1928
1929 // write matrix in ASCII file
1930 if (!fileflag) {
1931
1932 fp = fopen(filename, "w");
1933
1934 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1935
1936 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1937
1938 fprintf(fp, "%d%d%d%d\n", fileflag, fileflag, fileflag, fileflag);
1939
1940 fprintf(fp, "%d%d%d\n", matrixflag, (int)sizeof(INT), (int)sizeof(REAL));
1941
1942 switch (matrixflag) {
1943 case 1:
1944 fasp_dcsr_write_s(fp, (dCSRmat*)A);
1945 break;
1946 case 2:
1947 fasp_dbsr_write_s(fp, (dBSRmat*)A);
1948 break;
1949 case 3:
1950 fasp_dstr_write_s(fp, (dSTRmat*)A);
1951 break;
1952 default:
1953 printf("### WARNING: Unknown matrix flag %d\n", matrixflag);
1954 }
1955 fclose(fp);
1956 return;
1957 }
1958
1959 // write matrix in binary file
1960 fp = fopen(filename, "wb");
1961
1962 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filename);
1963
1964 printf("%s: writing to file %s ...\n", __FUNCTION__, filename);
1965
1966 INT putflag = fileflag * 100 + sizeof(INT) * 10 + sizeof(REAL);
1967 fwrite(&putflag, sizeof(INT), 1, fp);
1968
1969 switch (matrixflag) {
1970 case 1:
1971 fasp_dcsr_write_b(fp, (dCSRmat*)A);
1972 break;
1973 case 2:
1974 fasp_dbsr_write_b(fp, (dBSRmat*)A);
1975 break;
1976 case 3:
1977 fasp_dstr_write_b(fp, (dSTRmat*)A);
1978 break;
1979 default:
1980 printf("### WARNING: Unknown matrix flag %d\n", matrixflag);
1981 }
1982
1983 fclose(fp);
1984}
1985
2011void fasp_vector_read(const char* filerhs, void* b)
2012{
2013 int index, flag;
2014 SHORT EndianFlag;
2015 size_t status;
2016
2017 FILE* fp = fopen(filerhs, "rb");
2018
2019 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filerhs);
2020
2021 printf("%s: reading file %s ...\n", __FUNCTION__, filerhs);
2022
2023 status = fread(&index, sizeof(INT), 1, fp);
2024 fasp_chkerr(status, filerhs);
2025
2026 // vector stored in ASCII
2027 if (index == 808464432) {
2028
2029 fclose(fp);
2030 fp = fopen(filerhs, "r");
2031
2032 if (!fscanf(fp, "%d\n", &flag))
2033 printf("### ERROR: File format problem in %s!\n", __FUNCTION__);
2034 // TODO: Check why skip this flag ??? --Chensong
2035
2036 if (!fscanf(fp, "%d\n", &flag))
2037 printf("### ERROR: File format problem in %s!\n", __FUNCTION__);
2038 flag = (int)flag / 100;
2039
2040 switch (flag) {
2041 case 1:
2042 fasp_dvec_read_s(fp, (dvector*)b);
2043 break;
2044 case 2:
2045 fasp_ivec_read_s(fp, (ivector*)b);
2046 break;
2047 case 3:
2048 fasp_dvecind_read_s(fp, (dvector*)b);
2049 break;
2050 case 4:
2051 fasp_ivecind_read_s(fp, (ivector*)b);
2052 break;
2053 }
2054 fclose(fp);
2055 return;
2056 }
2057
2058 // vector stored in binary
2059 EndianFlag = index;
2060 status = fread(&index, sizeof(INT), 1, fp);
2061 fasp_chkerr(status, filerhs);
2062
2063 index = endian_convert_int(index, sizeof(INT), EndianFlag);
2064 flag = (int)index / 100;
2065 ilength = (int)(index - 100 * flag) / 10;
2066 dlength = index % 10;
2067
2068 switch (flag) {
2069 case 1:
2070 fasp_dvec_read_b(fp, (dvector*)b, EndianFlag);
2071 break;
2072 case 2:
2073 fasp_ivec_read_b(fp, (ivector*)b, EndianFlag);
2074 break;
2075 case 3:
2076 fasp_dvecind_read_b(fp, (dvector*)b, EndianFlag);
2077 break;
2078 case 4:
2079 fasp_ivecind_read_b(fp, (ivector*)b, EndianFlag);
2080 break;
2081 default:
2082 printf("### ERROR: Unknown flag %d in %s!\n", flag, filerhs);
2083 fasp_chkerr(ERROR_WRONG_FILE, __FUNCTION__);
2084 }
2085
2086 fclose(fp);
2087}
2088
2119void fasp_vector_write(const char* filerhs, void* b, const INT flag)
2120{
2121
2122 INT fileflag, vectorflag;
2123 FILE* fp;
2124
2125 fileflag = (int)flag / 10;
2126 vectorflag = (int)flag % 10;
2127
2128 // write vector in ASCII
2129 if (!fileflag) {
2130 fp = fopen(filerhs, "w");
2131
2132 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filerhs);
2133
2134 printf("%s: writing to file %s ...\n", __FUNCTION__, filerhs);
2135
2136 fprintf(fp, "%d%d%d%d\n", fileflag, fileflag, fileflag, fileflag);
2137
2138 fprintf(fp, "%d%d%d\n", vectorflag, (int)sizeof(INT), (int)sizeof(REAL));
2139
2140 switch (vectorflag) {
2141 case 1:
2142 fasp_dvec_write_s(fp, (dvector*)b);
2143 break;
2144 case 2:
2145 fasp_ivec_write_s(fp, (ivector*)b);
2146 break;
2147 case 3:
2148 fasp_dvecind_write_s(fp, (dvector*)b);
2149 break;
2150 case 4:
2151 fasp_ivecind_write_s(fp, (ivector*)b);
2152 break;
2153 default:
2154 printf("### WARNING: Unknown vector flag %d\n", vectorflag);
2155 }
2156
2157 fclose(fp);
2158 return;
2159 }
2160
2161 // write vector in binary
2162 fp = fopen(filerhs, "wb");
2163
2164 if (fp == NULL) fasp_chkerr(ERROR_OPEN_FILE, filerhs);
2165
2166 printf("%s: writing to file %s ...\n", __FUNCTION__, filerhs);
2167
2168 INT putflag = vectorflag * 100 + sizeof(INT) * 10 + sizeof(REAL);
2169 fwrite(&putflag, sizeof(INT), 1, fp);
2170
2171 switch (vectorflag) {
2172 case 1:
2173 fasp_dvec_write_b(fp, (dvector*)b);
2174 break;
2175 case 2:
2176 fasp_ivec_write_b(fp, (ivector*)b);
2177 break;
2178 case 3:
2179 fasp_dvecind_write_b(fp, (dvector*)b);
2180 break;
2181 case 4:
2182 fasp_ivecind_write_b(fp, (ivector*)b);
2183 break;
2184 default:
2185 printf("### WARNING: Unknown vector flag %d\n", vectorflag);
2186 }
2187
2188 fclose(fp);
2189}
2190
2206void fasp_hb_read(const char* input_file, dCSRmat* A, dvector* b)
2207{
2208 //-------------------------
2209 // Setup local variables
2210 //-------------------------
2211 // variables for FASP
2212 dCSRmat tempA;
2213
2214 // variables for hb_io
2215
2216 int* colptr = NULL;
2217 double* exact = NULL;
2218 double* guess = NULL;
2219 int i;
2220 int indcrd;
2221 char* indfmt = NULL;
2222 FILE* input;
2223 int j;
2224 char* key = NULL;
2225 char* mxtype = NULL;
2226 int ncol;
2227 int neltvl;
2228 int nnzero;
2229 int nrhs;
2230 int nrhsix;
2231 int nrow;
2232 int ptrcrd;
2233 char* ptrfmt = NULL;
2234 int rhscrd;
2235 char* rhsfmt = NULL;
2236 int* rhsind = NULL;
2237 int* rhsptr = NULL;
2238 char* rhstyp = NULL;
2239 double* rhsval = NULL;
2240 double* rhsvec = NULL;
2241 int* rowind = NULL;
2242 char* title = NULL;
2243 int totcrd;
2244 int valcrd;
2245 char* valfmt = NULL;
2246 double* values = NULL;
2247
2248 printf("\n");
2249 printf("HB_FILE_READ reads all the data in an HB file.\n");
2250
2251 printf("\n");
2252 printf("Reading the file '%s'\n", input_file);
2253
2254 input = fopen(input_file, "rt");
2255
2256 if (!input) {
2257 printf("### ERROR: Fail to open the file [%s]\n", input_file);
2258 fasp_chkerr(ERROR_OPEN_FILE, __FUNCTION__);
2259 }
2260
2261 //-------------------------
2262 // Reading...
2263 //-------------------------
2264 hb_file_read(input, &title, &key, &totcrd, &ptrcrd, &indcrd, &valcrd, &rhscrd,
2265 &mxtype, &nrow, &ncol, &nnzero, &neltvl, &ptrfmt, &indfmt, &valfmt,
2266 &rhsfmt, &rhstyp, &nrhs, &nrhsix, &colptr, &rowind, &values, &rhsval,
2267 &rhsptr, &rhsind, &rhsvec, &guess, &exact);
2268
2269 //-------------------------
2270 // Printing if needed
2271 //-------------------------
2272#if DEBUG_MODE > PRINT_MIN
2273 /*
2274 Print out the header information.
2275 */
2276 hb_header_print(title, key, totcrd, ptrcrd, indcrd, valcrd, rhscrd, mxtype, nrow,
2277 ncol, nnzero, neltvl, ptrfmt, indfmt, valfmt, rhsfmt, rhstyp, nrhs,
2278 nrhsix);
2279 /*
2280 Print the structure information.
2281 */
2282 hb_structure_print(ncol, mxtype, nnzero, neltvl, colptr, rowind);
2283
2284 /*
2285 Print the values.
2286 */
2287 hb_values_print(ncol, colptr, mxtype, nnzero, neltvl, values);
2288
2289 if (0 < rhscrd) {
2290 /*
2291 Print a bit of the right hand sides.
2292 */
2293 if (rhstyp[0] == 'F') {
2294 r8mat_print_some(nrow, nrhs, rhsval, 1, 1, 5, 5, " Part of RHS");
2295 } else if (rhstyp[0] == 'M' && mxtype[2] == 'A') {
2296 i4vec_print_part(nrhs + 1, rhsptr, 10, " Part of RHSPTR");
2297 i4vec_print_part(nrhsix, rhsind, 10, " Part of RHSIND");
2298 r8vec_print_part(nrhsix, rhsvec, 10, " Part of RHSVEC");
2299 } else if (rhstyp[0] == 'M' && mxtype[2] == 'E') {
2300 r8mat_print_some(nnzero, nrhs, rhsval, 1, 1, 5, 5, " Part of RHS");
2301 }
2302 /*
2303 Print a bit of the starting guesses.
2304 */
2305 if (rhstyp[1] == 'G') {
2306 r8mat_print_some(nrow, nrhs, guess, 1, 1, 5, 5, " Part of GUESS");
2307 }
2308 /*
2309 Print a bit of the exact solutions.
2310 */
2311 if (rhstyp[2] == 'X') {
2312 r8mat_print_some(nrow, nrhs, exact, 1, 1, 5, 5, " Part of EXACT");
2313 }
2314 }
2315#endif
2316
2317 //-------------------------
2318 // Closing
2319 //-------------------------
2320 fclose(input);
2321
2322 //-------------------------
2323 // Convert to FASP format
2324 //-------------------------
2325
2326 // convert matrix
2327 if (ncol != nrow) {
2328 printf("### ERROR: The matrix is not square! [%s]\n", __FUNCTION__);
2329 goto FINISHED;
2330 }
2331
2332 tempA = fasp_dcsr_create(nrow, ncol, nnzero);
2333
2334 for (i = 0; i <= ncol; i++) tempA.IA[i] = colptr[i] - 1;
2335 for (i = 0; i < nnzero; i++) tempA.JA[i] = rowind[i] - 1;
2336 fasp_darray_cp(nnzero, values, tempA.val);
2337
2338 // if the matrix is symmeric
2339 if (mxtype[1] == 'S') {
2340
2341 // A = A'+ A
2342 dCSRmat tempA_tran;
2343 fasp_dcsr_trans(&tempA, &tempA_tran);
2344 fasp_blas_dcsr_add(&tempA, 1.0, &tempA_tran, 1.0, A);
2345 fasp_dcsr_free(&tempA);
2346 fasp_dcsr_free(&tempA_tran);
2347
2348 // modify diagonal entries
2349 for (i = 0; i < A->row; i++) {
2350
2351 for (j = A->IA[i]; j < A->IA[i + 1]; j++) {
2352
2353 if (A->JA[j] == i) {
2354 A->val[j] = A->val[j] / 2;
2355 break;
2356 }
2357 }
2358 }
2359 }
2360 // if the matrix is not symmetric
2361 else {
2362 fasp_dcsr_trans(&tempA, A);
2363 fasp_dcsr_free(&tempA);
2364 }
2365
2366 // convert right hand side
2367
2368 if (nrhs == 0) {
2369
2370 printf("### ERROR: No right hand side! [%s]\n", __FUNCTION__);
2371 goto FINISHED;
2372 } else if (nrhs > 1) {
2373
2374 printf("### ERROR: More than one right hand side! [%s]\n", __FUNCTION__);
2375 goto FINISHED;
2376 } else {
2377
2378 fasp_dvec_alloc(nrow, b);
2379 fasp_darray_cp(nrow, rhsval, b->val);
2380 }
2381
2382 //-------------------------
2383 // Cleanning
2384 //-------------------------
2385FINISHED:
2386 if (colptr) free(colptr);
2387 if (exact) free(exact);
2388 if (guess) free(guess);
2389 if (rhsind) free(rhsind);
2390 if (rhsptr) free(rhsptr);
2391 if (rhsval) free(rhsval);
2392 if (rhsvec) free(rhsvec);
2393 if (rowind) free(rowind);
2394 if (values) free(values);
2395
2396 return;
2397}
2398
2399/*---------------------------------*/
2400/*-- End of File --*/
2401/*---------------------------------*/
void fasp_darray_cp(const INT n, const REAL *x, REAL *y)
Copy an array to the other y=x.
Definition: AuxArray.c:210
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_ivec_alloc(const INT m, ivector *u)
Create vector data space of INT type.
Definition: AuxVector.c:125
void fasp_dvec_alloc(const INT m, dvector *u)
Create dvector data space of REAL type.
Definition: AuxVector.c:105
SHORT fasp_format_dcoo_dcsr(const dCOOmat *A, dCSRmat *B)
Transform a REAL matrix from its IJ format to its CSR format.
Definition: BlaFormat.c:36
void fasp_dbsr_write_coo(const char *filename, const dBSRmat *A)
Print out a dBSRmat matrix in coordinate format for matlab spy.
Definition: BlaIO.c:1568
void fasp_ivecind_read(const char *filename, ivector *b)
Read b from matrix disk file.
Definition: BlaIO.c:989
void fasp_dcsrvec_write2(const char *filemat, const char *filerhs, dCSRmat *A, dvector *b)
Write A and b to two separate disk files.
Definition: BlaIO.c:1145
void fasp_dcoo_read1(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format – indices starting from 1.
Definition: BlaIO.c:384
void fasp_dstr_print(const dSTRmat *A)
Print out a dSTRmat matrix in coordinate format.
Definition: BlaIO.c:1701
void fasp_matrix_write(const char *filename, void *A, const INT flag)
write matrix from different kinds of formats from both ASCII and binary files
Definition: BlaIO.c:1921
void fasp_dcoo_print(const dCOOmat *A)
Print out a dCOOmat matrix in coordinate format.
Definition: BlaIO.c:1545
void fasp_dcsrvec_write1(const char *filename, dCSRmat *A, dvector *b)
Write A and b to a SINGLE disk file.
Definition: BlaIO.c:1079
int dlength
Definition: BlaIO.c:24
void fasp_dcoo_write(const char *filename, dCSRmat *A)
Write a matrix to disk file in IJ format (coordinate format)
Definition: BlaIO.c:1207
void fasp_dcsr_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format.
Definition: BlaIO.c:252
void fasp_dcsrvec_read1(const char *filename, dCSRmat *A, dvector *b)
Read A and b from a SINGLE disk file.
Definition: BlaIO.c:63
void fasp_ivec_write(const char *filename, ivector *vec)
Write a ivector to disk file in coordinate format.
Definition: BlaIO.c:1452
void fasp_matrix_read(const char *filename, void *A)
Read matrix from different kinds of formats from both ASCII and binary files.
Definition: BlaIO.c:1735
void fasp_dcsrvec_read2(const char *filemat, const char *filerhs, dCSRmat *A, dvector *b)
Read A and b from two separate disk files.
Definition: BlaIO.c:164
void fasp_matrix_read_bin(const char *filename, void *A)
Read matrix in binary format.
Definition: BlaIO.c:1849
void fasp_dcoovec_bin_read(const char *fni, const char *fnj, const char *fna, const char *fnb, dCSRmat *A, dvector *b)
Read A from matrix disk files in IJ format (three binary files)
Definition: BlaIO.c:437
void fasp_dvec_read(const char *filename, dvector *b)
Read b from a disk file in array format.
Definition: BlaIO.c:938
void fasp_dvec_print(const INT n, dvector *u)
Print first n entries of a vector of REAL type.
Definition: BlaIO.c:1482
void fasp_dvec_write(const char *filename, dvector *vec)
Write a dvector to disk file.
Definition: BlaIO.c:1388
void fasp_dmtx_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in MatrixMarket general format.
Definition: BlaIO.c:567
void fasp_dcoo_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format – indices starting from 0.
Definition: BlaIO.c:332
void fasp_vector_write(const char *filerhs, void *b, const INT flag)
write RHS vector from different kinds of formats in both ASCII and binary files
Definition: BlaIO.c:2119
void fasp_ivec_read(const char *filename, ivector *b)
Read b from a disk file in array format.
Definition: BlaIO.c:1029
void fasp_dcsr_write_mtx(const char *filename, const dCSRmat *A)
Print out a dCSRmat matrix in coordinate format for MatrixMarket.
Definition: BlaIO.c:1664
void fasp_dcsr_print(const dCSRmat *A)
Print out a dCSRmat matrix in coordinate format.
Definition: BlaIO.c:1523
void fasp_dbsr_print(const char *filename, dBSRmat *A)
Print a dBSRmat to a disk file in a readable format.
Definition: BlaIO.c:1292
void fasp_dcoo_shift_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in IJ format – indices starting from 0.
Definition: BlaIO.c:514
void fasp_dmtxsym_read(const char *filename, dCSRmat *A)
Read A from matrix disk file in MatrixMarket sym format.
Definition: BlaIO.c:624
int ilength
Definition: BlaIO.c:23
void fasp_dbsr_write(const char *filename, dBSRmat *A)
Write a dBSRmat to a disk file.
Definition: BlaIO.c:1336
void fasp_dstr_read(const char *filename, dSTRmat *A)
Read A from a disk file in dSTRmat format.
Definition: BlaIO.c:699
void fasp_dbsr_read(const char *filename, dBSRmat *A)
Read A from a disk file in dBSRmat format.
Definition: BlaIO.c:807
void fasp_dstr_write(const char *filename, dSTRmat *A)
Write a dSTRmat to a disk file.
Definition: BlaIO.c:1241
void fasp_ivec_print(const INT n, ivector *u)
Print first n entries of a vector of INT type.
Definition: BlaIO.c:1503
void fasp_hb_read(const char *input_file, dCSRmat *A, dvector *b)
Read matrix and right-hans side from a HB format file.
Definition: BlaIO.c:2206
void fasp_vector_read(const char *filerhs, void *b)
Read RHS vector from different kinds of formats in ASCII or binary files.
Definition: BlaIO.c:2011
void fasp_dcsr_write_coo(const char *filename, const dCSRmat *A)
Print out a dCSRmat matrix in coordinate format for matlab spy.
Definition: BlaIO.c:1623
void fasp_dvecind_write(const char *filename, dvector *vec)
Write a dvector to disk file in coordinate format.
Definition: BlaIO.c:1420
void fasp_dvecind_read(const char *filename, dvector *b)
Read b from matrix disk file.
Definition: BlaIO.c:887
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: BlaSparseBSR.c:96
void fasp_dcoo_free(dCOOmat *A)
Free IJ sparse matrix data memory space.
Definition: BlaSparseCOO.c:102
dCOOmat fasp_dcoo_create(const INT m, const INT n, const INT nnz)
Create IJ sparse matrix data memory space.
Definition: BlaSparseCOO.c:42
dCSRmat fasp_dcsr_create(const INT m, const INT n, const INT nnz)
Create CSR sparse matrix data memory space.
Definition: BlaSparseCSR.c:47
void fasp_dcsr_free(dCSRmat *A)
Free CSR sparse matrix data memory space.
Definition: BlaSparseCSR.c:184
INT fasp_dcsr_trans(const dCSRmat *A, dCSRmat *AT)
Find transpose of dCSRmat matrix A.
Definition: BlaSparseCSR.c:952
SHORT fasp_blas_dcsr_add(const dCSRmat *A, const REAL alpha, const dCSRmat *B, const REAL beta, dCSRmat *C)
compute C = alpha*A + beta*B in CSR format
Definition: BlaSpmvCSR.c:60
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 ERROR_OPEN_FILE
Definition: fasp_const.h:22
#define ERROR_WRONG_FILE
Definition: fasp_const.h:23
#define BIGREAL
Some global constants.
Definition: fasp_const.h:255
#define ERROR_INPUT_PAR
Definition: fasp_const.h:24
#define ERROR_MAT_SIZE
Definition: fasp_const.h:26
Block sparse row storage matrix of REAL type.
Definition: fasp_block.h:34
INT COL
number of cols of sub-blocks in matrix A, N
Definition: fasp_block.h:40
INT NNZ
number of nonzero sub-blocks in matrix A, NNZ
Definition: fasp_block.h:43
REAL * val
Definition: fasp_block.h:57
INT nb
dimension of each sub-block
Definition: fasp_block.h:46
INT * IA
integer array of row pointers, the size is ROW+1
Definition: fasp_block.h:60
INT ROW
number of rows of sub-blocks in matrix A, M
Definition: fasp_block.h:37
INT * JA
Definition: fasp_block.h:64
INT storage_manner
storage manner for each sub-block
Definition: fasp_block.h:49
Sparse matrix of REAL type in COO (IJ) format.
Definition: fasp.h:221
INT * colind
integer array of column indices, the size is nnz
Definition: fasp.h:236
INT col
column of matrix A, n
Definition: fasp.h:227
INT * rowind
integer array of row indices, the size is nnz
Definition: fasp.h:233
REAL * val
nonzero entries of A
Definition: fasp.h:239
INT row
row number of matrix A, m
Definition: fasp.h:224
INT nnz
number of nonzero entries
Definition: fasp.h:230
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
Structure matrix of REAL type.
Definition: fasp.h:316
INT * offsets
offsets of the off-diagonals (length is nband)
Definition: fasp.h:343
INT nx
number of grids in x direction
Definition: fasp.h:319
INT nxy
number of grids on x-y plane
Definition: fasp.h:328
INT ny
number of grids in y direction
Definition: fasp.h:322
INT nband
number of off-diag bands
Definition: fasp.h:340
REAL * diag
diagonal entries (length is ngrid*(nc^2))
Definition: fasp.h:337
INT ngrid
number of grids
Definition: fasp.h:334
INT nc
size of each block (number of components)
Definition: fasp.h:331
INT nz
number of grids in z direction
Definition: fasp.h:325
REAL ** offdiag
off-diagonal entries (dimension is nband * [(ngrid-|offsets|) * nc^2])
Definition: fasp.h:346
Vector with n entries of REAL type.
Definition: fasp.h:354
REAL * val
actual vector entries
Definition: fasp.h:360
INT row
number of rows
Definition: fasp.h:357
Vector with n entries of INT type.
Definition: fasp.h:368
INT row
number of rows
Definition: fasp.h:371
INT * val
actual vector entries
Definition: fasp.h:374