00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "EpetraExt_CrsMatrixIn.h"
00029 #include "Epetra_Comm.h"
00030 #include "Epetra_CrsMatrix.h"
00031 #include "Epetra_Map.h"
00032 #include "Epetra_IntVector.h"
00033 #include "Epetra_IntSerialDenseVector.h"
00034 #include "Epetra_Import.h"
00035 #include "Epetra_Util.h"
00036
00037 using namespace EpetraExt;
00038 namespace EpetraExt {
00039
00040 int MatrixMarketFileToCrsMatrix( const char *filename, const Epetra_Comm & comm, Epetra_CrsMatrix * & A) {
00041
00042 EPETRA_CHK_ERR(MatrixMarketFileToCrsMatrixHandle(filename, comm, A));
00043 return(0);
00044 }
00045
00046 int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Map & rowMap,
00047 const Epetra_Map& rangeMap, const Epetra_Map& domainMap, Epetra_CrsMatrix * & A) {
00048 EPETRA_CHK_ERR(MatrixMarketFileToCrsMatrixHandle(filename, rowMap.Comm(), A, &rowMap, 0, &rangeMap, &domainMap));
00049 return(0);
00050 }
00051
00052 int MatrixMarketFileToCrsMatrix( const char *filename, const Epetra_Map & rowMap, Epetra_CrsMatrix * & A) {
00053
00054 EPETRA_CHK_ERR(MatrixMarketFileToCrsMatrixHandle(filename, rowMap.Comm(), A, &rowMap));
00055 return(0);
00056 }
00057
00058 int MatrixMarketFileToCrsMatrix( const char *filename, const Epetra_Map & rowMap, const Epetra_Map & colMap, Epetra_CrsMatrix * & A) {
00059
00060 EPETRA_CHK_ERR(MatrixMarketFileToCrsMatrixHandle(filename, rowMap.Comm(), A, &rowMap, &colMap));
00061 return(0);
00062 }
00063
00064 int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Map & rowMap, const Epetra_Map & colMap,
00065 const Epetra_Map& rangeMap, const Epetra_Map& domainMap, Epetra_CrsMatrix * & A) {
00066 EPETRA_CHK_ERR(MatrixMarketFileToCrsMatrixHandle(filename, rowMap.Comm(), A, &rowMap, &colMap, &rangeMap, &domainMap));
00067 return(0);
00068 }
00069
00070 int MatrixMarketFileToCrsMatrixHandle(const char *filename,
00071 const Epetra_Comm & comm,
00072 Epetra_CrsMatrix * & A,
00073 const Epetra_Map * rowMap,
00074 const Epetra_Map * colMap,
00075 const Epetra_Map * rangeMap,
00076 const Epetra_Map * domainMap)
00077 {
00078
00079 const int lineLength = 1025;
00080 const int tokenLength = 35;
00081 char line[lineLength];
00082 char token1[tokenLength];
00083 char token2[tokenLength];
00084 char token3[tokenLength];
00085 char token4[tokenLength];
00086 char token5[tokenLength];
00087 int M, N, NZ;
00088
00089
00090 if ((domainMap!=0 && rangeMap==0) || (domainMap==0 && rangeMap!=0)) {EPETRA_CHK_ERR(-3);}
00091
00092
00093
00094 if (domainMap!=0) {
00095 if (!domainMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
00096 if (!rangeMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
00097 }
00098 else {
00099
00100 if (rowMap!=0) {
00101 if (!rowMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
00102 }
00103 }
00104
00105
00106 FILE * handle = 0;
00107
00108 handle = fopen(filename,"r");
00109 if (handle == 0)
00110 EPETRA_CHK_ERR(-1);
00111
00112
00113 if(fgets(line, lineLength, handle)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00114 if(sscanf(line, "%s %s %s %s %s", token1, token2, token3, token4, token5 )==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00115 if (strcmp(token1, "%%MatrixMarket") ||
00116 strcmp(token2, "matrix") ||
00117 strcmp(token3, "coordinate") ||
00118 strcmp(token4, "real") ||
00119 strcmp(token5, "general")) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00120
00121
00122 do {
00123 if(fgets(line, lineLength, handle)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00124 } while (line[0] == '%');
00125
00126
00127 if(sscanf(line, "%d %d %d", &M, &N, &NZ)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00128
00129
00130
00131 if (rowMap!=0 && colMap !=0)
00132 A = new Epetra_CrsMatrix(Copy, *rowMap, *colMap, 0);
00133 else if (rowMap!=0)
00134 A = new Epetra_CrsMatrix(Copy, *rowMap, 0);
00135 else {
00136 Epetra_Map newRowMap(M,0, comm);
00137 A = new Epetra_CrsMatrix(Copy, newRowMap, 0);
00138 }
00139
00140 int I, J;
00141 double V;
00142 const Epetra_Map & rowMap1 = A->RowMap();
00143 const Epetra_Map & colMap1 = A->ColMap();
00144 int ioffset = rowMap1.IndexBase()-1;
00145 int joffset = colMap1.IndexBase()-1;
00146
00147 for (int i=0; i<NZ; i++) {
00148 if(fgets(line, lineLength, handle)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);};
00149 if(sscanf(line, "%d %d %lg\n", &I, &J, &V)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00150 I+=ioffset; J+=joffset;
00151 if (rowMap1.MyGID(I)) {
00152 int ierr = A->InsertGlobalValues(I, 1, &V, &J);
00153 if (ierr<0) EPETRA_CHK_ERR(ierr);
00154 }
00155 }
00156
00157 if (rangeMap != 0 && domainMap != 0) {
00158 A->FillComplete(*domainMap, *rangeMap);
00159 }
00160 else {
00161 A->FillComplete();
00162 }
00163
00164 if (handle!=0) fclose(handle);
00165 return(0);
00166 }
00167
00168 int MatlabFileToCrsMatrix(const char *filename,
00169 const Epetra_Comm & comm,
00170 Epetra_CrsMatrix * & A)
00171 {
00172 const int lineLength = 1025;
00173 char line[lineLength];
00174 int I, J;
00175 double V;
00176
00177 FILE * handle = 0;
00178
00179 handle = fopen(filename,"r");
00180 if (handle == 0)
00181 EPETRA_CHK_ERR(-1);
00182
00183 int numGlobalRows = 0;
00184 int numGlobalCols = 0;
00185 while(fgets(line, lineLength, handle)!=0) {
00186 if(sscanf(line, "%d %d %lg\n", &I, &J, &V)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00187 if (I>numGlobalRows) numGlobalRows = I;
00188 if (J>numGlobalCols) numGlobalCols = J;
00189 }
00190
00191 if (handle!=0) fclose(handle);
00192
00193 Epetra_Map domainMap(numGlobalRows, 0, comm);
00194 Epetra_Map rangeMap(numGlobalCols, 0, comm);
00195 A = new Epetra_CrsMatrix(Copy, rangeMap, 0);
00196
00197
00198 const Epetra_Map & rowMap1 = A->RowMap();
00199
00200 handle = 0;
00201
00202 handle = fopen(filename,"r");
00203 if (handle == 0)
00204 EPETRA_CHK_ERR(-1);
00205
00206 while (fgets(line, lineLength, handle)!=0) {
00207 if(sscanf(line, "%d %d %lg\n", &I, &J, &V)==0) {if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
00208 I--; J--;
00209 if (rowMap1.MyGID(I)) {
00210 int ierr = A->InsertGlobalValues(I, 1, &V, &J);
00211 if (ierr<0) EPETRA_CHK_ERR(ierr);
00212 }
00213 }
00214
00215 EPETRA_CHK_ERR(A->FillComplete(domainMap, rangeMap));
00216
00217 if (handle!=0) fclose(handle);
00218 return(0);
00219 }
00220 }
00221