Memosa-FVM  0.2
MMReader.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "MMReader.h"
6 #include "Array.h"
7 #include "CRConnectivity.h"
8 #include "LinearSystem.h"
9 
10 
11 MMReader::MMReader(const string& matrixFileName, const string& rhsFileName) :
12  Reader(matrixFileName),
13  _rhsFileName(rhsFileName),
14  _field("test"),
15  _site(),
16  _cm()
17 {}
18 
20 {}
21 
22 void
23 MMReader::readHeader(int& nRows, int& nCols,
24  int& nNonZeroes, bool& isSymmetric)
25 {
26  if (_fp==0)
27  throw CException("cannot open matrix file ");
28 
29  char buf[32];
30 
31  fscanf(_fp,"%s",buf);
32 
33  string h1(buf);
34  if (h1 == "%%")
35  {
36  fscanf(_fp,"%s",buf);
37  string h2(buf);
38  if (h2 != "MatrixMarket")
39  throw CException("not a MatrixMarket file");
40  }
41  else if (h1 != "%%MatrixMarket")
42  throw CException("not a MatrixMarket file");
43 
44  fscanf(_fp,"%s",buf);
45  string matrixType(buf);
46 
47  fscanf(_fp,"%s",buf);
48  string coordinate(buf);
49 
50  fscanf(_fp,"%s",buf);
51  string ftype(buf);
52 
53  fscanf(_fp,"%s",buf);
54  string symm(buf);
55 
56  isSymmetric = false;
57 
58 
59  if (matrixType != "matrix")
60  throw CException("not a MatrixMarket file");
61 
62  if (coordinate != "coordinate")
63  throw CException("not a sparse matrix");
64 
65  if (ftype != "real")
66  throw CException("not a real matrix");
67 
68  if (symm == "symmetric")
69  isSymmetric = true;
70  else if (symm != "general")
71  throw CException("not symmetric or general matrix");
72 
73  fscanf(_fp, "%d %d %d", &nRows, &nCols, &nNonZeroes);
74 
75  if (nRows != nCols)
76  throw CException("not a square matrix");
77 }
78 
79 shared_ptr<LinearSystem>
81 {
82  int nRows, nCols, nNonZeroes;
83  bool isSymmetric;
84 
85  readHeader(nRows,nCols,nNonZeroes,isSymmetric);
86 
87  cout << " nRow " << nRows << " x " << nCols
88  << " matrix with " << nNonZeroes << " entries "
89  << endl;
90 
91  cout << "reading sizes" << endl;
92 
93  _site = shared_ptr<StorageSite>(new StorageSite(nRows,0));
94 
95  _cm = shared_ptr<CRConnectivity>(new CRConnectivity(*_site,*_site));
96 
97 
98  _cm->initCount();
99 
100  for(int nr=0; nr<nNonZeroes; nr++)
101  {
102  int i, j;
103  double c;
104  fscanf(_fp,"%d %d %lf",&i,&j,&c);
105  i-=1;
106  j-=1;
107  if (i!=j)
108  {
109  _cm->addCount(i,1);
110  if (isSymmetric)
111  _cm->addCount(j,1);
112  }
113  }
114 
115  _cm->finishCount();
116 
117  shared_ptr<MatrixType> m(new MatrixType(*_cm));
118 
119  MultiField::ArrayIndex rowI(&_field,_site.get());
120 
121  shared_ptr<Array<double> > xPtr(new Array<double>(nRows));
122 
123  xPtr->zero();
124 
125  shared_ptr<LinearSystem> ls(new LinearSystem());
126  ls->getX().addArray(rowI,xPtr);
127  ls->getMatrix().addMatrix(rowI,rowI,m);
128 
129  ls->initAssembly();
130 
131  Array<double>& diag = m->getDiag();
132  Array<double>& offDiag = m->getOffDiag();
133 
134  // rewind file
135  cout << "rewinding and reading coeffs" << endl;
136 
137  resetFilePtr();
138 
139  readHeader(nRows,nCols,nNonZeroes,isSymmetric);
140 
141  for(int nr=0; nr<nNonZeroes; nr++)
142  {
143  int i, j;
144  double c;
145  fscanf(_fp,"%d %d %lf",&i,&j,&c);
146  i-=1;
147  j-=1;
148  if (i!=j)
149  {
150  int pos = _cm->add(i,j);
151  offDiag[pos] = c;
152 
153  if (isSymmetric)
154  {
155  pos = _cm->add(j,i);
156  offDiag[pos] = c;
157  }
158  }
159  else
160  diag[i] = c;
161  }
162 
163  _cm->finishAdd();
164  cout << "finished reading " << endl;
165  close();
166 
167 
168  //ls->getB().addArray(rowI,bPtr);
169 
170 
171  Array<double>& b = dynamic_cast<Array<double>&>(ls->getB()[rowI]);
172 
173  FILE *bFile = fopen(_rhsFileName.c_str(), "rb");
174 
175  for(int i=0; i<nRows; i++)
176  {
177  double r;
178  fscanf(bFile,"%lf",&r);
179  b[i]=-r;
180  }
181 
182  ls->initSolve();
183  return ls;
184 }
Definition: Reader.h:15
void resetFilePtr()
Definition: Reader.cpp:27
shared_ptr< CRConnectivity > _cm
Definition: MMReader.h:33
void readHeader(int &nRows, int &nCols, int &nNonZeroes, bool &isSymmetric)
Definition: MMReader.cpp:23
FILE * _fp
Definition: Reader.h:26
pair< const Field *, const StorageSite * > ArrayIndex
Definition: MultiField.h:21
shared_ptr< LinearSystem > getLS()
Definition: MMReader.cpp:80
MMReader(const string &matrixFileName, const string &rhsFileName)
Definition: MMReader.cpp:11
Field _field
Definition: MMReader.h:31
CRMatrix< double, double, double > MatrixType
Definition: MMReader.h:20
void close()
Definition: Reader.cpp:64
const string _rhsFileName
Definition: MMReader.h:30
shared_ptr< StorageSite > _site
Definition: MMReader.h:32
virtual ~MMReader()
Definition: MMReader.cpp:19