Skip to content
Snippets Groups Projects
Commit 853853e6 authored by Oliver Sander's avatar Oliver Sander
Browse files

added a method which writes BCRSMatrices in a Matlab-readable format

[[Imported from SVN: r1642]]
parent 33793999
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "istlexception.hh" #include "istlexception.hh"
#include <dune/common/fvector.hh> #include <dune/common/fvector.hh>
#include <dune/common/fmatrix.hh> #include <dune/common/fmatrix.hh>
#include <dune/istl/bcrsmatrix.hh>
/** \file /** \file
...@@ -195,6 +196,56 @@ namespace Dune { ...@@ -195,6 +196,56 @@ namespace Dune {
s.setf(std::ios_base::fixed, std::ios_base::floatfield); s.setf(std::ios_base::fixed, std::ios_base::floatfield);
} }
/** \brief Writes sparse matrix in a Matlab-readable format
*
* This routine writes the argument BCRSMatrix to a file with the name given by
* the filename argument. The file format is ASCII, with no header, and three
* data columns. Each row describes a scalar matrix entry and consists of the
* matrix row and column numbers (both counted starting from 1), and the matrix
* entry. Such a file can be read from Matlab using the command
* \verbatim
new_mat = spconvert(load('filename'));
\endverbatim
*/
template <class BlockType>
void writeMatrixToMatlab(const BCRSMatrix<BlockType>& matrix, const std::string& filename)
{
FILE* fp = fopen(filename.c_str(), "w");
int blocksize = 3;
typedef typename BCRSMatrix<BlockType>::row_type RowType;
// Loop over all matrix rows
for (int rowIdx=0; rowIdx<matrix.N(); rowIdx++) {
const RowType& row = matrix[rowIdx];
typedef typename RowType::ConstIterator ColumnIterator;
ColumnIterator cIt = row.begin();
ColumnIterator cEndIt = row.end();
// Loop over all columns in this row
for (; cIt!=cEndIt; ++cIt) {
for (int i=0; i<blocksize; i++) {
for (int j=0; j<blocksize; j++) {
//+1 for Matlab numbering
fprintf(fp, "%d %d %g\n", rowIdx*blocksize + i + 1,
cIt.index()*blocksize + j + 1, (*cIt)[i][j]);
}
}
}
}
fclose(fp);
}
/** @} end documentation */ /** @} end documentation */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment