Skip to content
Snippets Groups Projects
Commit 6408dc90 authored by Christian Engwer's avatar Christian Engwer
Browse files

* improved ostream operator for spmatrix

* using exceptions instead of cerr

[[Imported from SVN: r802]]
parent caf36b0f
No related branches found
No related tags found
No related merge requests found
......@@ -146,6 +146,19 @@ namespace Dune {
*/
class MathError : public Exception {};
/*! default exception class for Range errors
This is the superclass for all errors which are caused because
the user tries to access data, that was not allocated before.
These can be problems like
- accessing array entries behind the last entry
- adding the fourth non zero entry in a sparse matrix
with only three non zero entries per row
*/
class RangeError : public Exception {};
} // end namespace
#endif
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <cmath>
#include <limits>
#include <dune/common/exceptions.hh>
namespace Dune
{
......@@ -184,8 +186,10 @@ namespace Dune
int whichCol = colIndex(row,col);
if(whichCol < 0)
{
std::cerr << "Error in SparseRowMatrix::set: Entry (" << row << ", " << col << ") "
<< "could neither be found nor newly allocated!\n";
DUNE_THROW(RangeError,
"Error in SparseRowMatrix::set: Entry ("
<< row << ", " << col << ") "
<< "could neither be found nor newly allocated!");
}
else
{
......@@ -197,14 +201,19 @@ namespace Dune
template <class T>
void SparseRowMatrix<T>::add(int row, int col, const T& val)
{
if (std::numeric_limits<T>::has_quiet_NaN &&
std::numeric_limits<T>::quiet_NaN() == val)
DUNE_THROW(MathError, "trying to add NAN to a matrix entry.");
if(std::abs(val) < EPS)
return;
int whichCol = colIndex(row,col);
if(whichCol < 0)
{
std::cerr << "Error in SparseRowMatrix::add: Entry (" << row << ", " << col << ") "
<< "could neither be found nor newly allocated!\n";
DUNE_THROW(RangeError,
"Error in SparseRowMatrix::add: Entry ("
<< row << ", " << col << ") "
<< "could neither be found nor newly allocated!");
}
else
{
......@@ -219,8 +228,10 @@ namespace Dune
int whichCol = colIndex(row,col);
if(whichCol < 0)
{
std::cerr << "Error in SparseRowMatrix::multScalar: Entry Entry (" << row << ", " << col << ") "
<< "could neither be found nor newly allocated!\n";
DUNE_THROW(RangeError,
"Error in SparseRowMatrix::multScalar: Entry Entry ("
<< row << ", " << col << ") "
<< "could neither be found nor newly allocated!");
}
else
{
......@@ -434,15 +445,21 @@ namespace Dune
template <class T>
void SparseRowMatrix<T>::print(std::ostream& s) const
void SparseRowMatrix<T>::print(std::ostream& s, int width) const
{
char txt[20];
for(int row=0; row<dim_[0]; row++)
{
for(int col=0; col<dim_[1]; col++)
{
s << (*this)(row,col) << " ";
T t = (*this)(row,col);
if (t == 0.0)
snprintf(txt, 20, "%*i.0 ", width+5, 0);
else
snprintf(txt, 20, "% 1.*e ", width, t);
s << txt;
}
s << "\n";
s << std::endl;
}
}
......@@ -495,7 +512,7 @@ namespace Dune
// non zero values before this line
nzval_.resize(dim_[0]+1);
for (int i=0; i<nzval_.size(); i++)
nzval_[i] = 3*i;
nzval_[i] = nz_*i;
// fill missing entries
for (int row = 0; row < dim_[0]; row++)
{
......
......@@ -209,7 +209,7 @@ namespace Dune
SparseRowMatrix<T> applyFromLeftAndRightTo(const SparseRowMatrix<T>& A) const;
//! Prints the complete matrix including the nonzero entries
void print (std::ostream& s) const;
void print (std::ostream& s, int width=3) const;
//! Just prints the nonzero entries
void printReal (std::ostream& s) const;
......
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