From 6408dc90a23d0cb6e3a3b9bba5e2d15379971e21 Mon Sep 17 00:00:00 2001
From: Christian Engwer <christi@dune-project.org>
Date: Mon, 4 Oct 2004 13:48:05 +0000
Subject: [PATCH] * improved ostream operator for spmatrix * using exceptions
 instead of cerr

[[Imported from SVN: r802]]
---
 common/exceptions.hh | 13 +++++++++++++
 fem/feop/spmatrix.cc | 37 +++++++++++++++++++++++++++----------
 fem/feop/spmatrix.hh |  2 +-
 3 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/common/exceptions.hh b/common/exceptions.hh
index 7a9085726..f42b740cd 100644
--- a/common/exceptions.hh
+++ b/common/exceptions.hh
@@ -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
diff --git a/fem/feop/spmatrix.cc b/fem/feop/spmatrix.cc
index 671bb3801..a4e97e5fa 100644
--- a/fem/feop/spmatrix.cc
+++ b/fem/feop/spmatrix.cc
@@ -1,6 +1,8 @@
 // -*- 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++)
     {
diff --git a/fem/feop/spmatrix.hh b/fem/feop/spmatrix.hh
index fd32fdb87..e94b127c6 100644
--- a/fem/feop/spmatrix.hh
+++ b/fem/feop/spmatrix.hh
@@ -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;
-- 
GitLab