On Thu, 10 Mar 2011, John Peterson wrote:

On Thu, Mar 10, 2011 at 9:09 AM, Tim Kroeger
<[email protected]> wrote:

What about this:

       SparseMatrix::print(std::ostream& os, const bool sparse=false);

Yeah, that's pretty much what I had in mind.

I've implemented this now, see attachment. It works for me, and I would like to check it in, unless there are still objections.

Note that for the complex case, I left the (wrong) implementation for the non-sparse case unchanged and added libmesh_not_implemented() for the sparse case. Everything remains downwards compatible that way, and who ever wants to fix the complex case can then add or not add the sparse case there.

Best Regards,

Tim

--
Dr. Tim Kroeger
CeVis -- Center of Complex Systems and Visualization
University of Bremen              [email protected]
Universitaetsallee 29             [email protected]
D-28359 Bremen                            Phone +49-421-218-59246
Germany                                   Fax   +49-421-218-59277
Index: include/numerics/sparse_matrix.h
===================================================================
--- include/numerics/sparse_matrix.h    (Revision 4241)
+++ include/numerics/sparse_matrix.h    (Arbeitskopie)
@@ -292,7 +292,7 @@
    * in a uniform style, regardless of matrix/solver
    * package being used.
    */
-  void print(std::ostream& os=libMesh::out) const;
+  void print(std::ostream& os=libMesh::out, const bool sparse=false) const;
 
   /**
    * Same as the print method above, but allows you
@@ -429,10 +429,15 @@
 // Full specialization for Complex datatypes
 template <>
 inline
-void SparseMatrix<Complex>::print(std::ostream& os) const
+  void SparseMatrix<Complex>::print(std::ostream& os, const bool sparse) const
 {
   // std::complex<>::operator<<() is defined, but use this form
 
+  if(sparse)
+    {
+      libmesh_not_implemented();
+    }
+
   os << "Real part:" << std::endl;
   for (unsigned int i=0; i<this->m(); i++)
     {
Index: src/numerics/sparse_matrix.C
===================================================================
--- src/numerics/sparse_matrix.C        (Revision 4241)
+++ src/numerics/sparse_matrix.C        (Arbeitskopie)
@@ -118,7 +118,7 @@
 
 template <typename T>
 inline
-void SparseMatrix<T>::print(std::ostream& os) const
+void SparseMatrix<T>::print(std::ostream& os, const bool sparse) const
 {
   parallel_only();
 
@@ -138,9 +138,23 @@
       for (unsigned int i=this->_dof_map->first_dof();
            i!=this->_dof_map->end_dof(); ++i)
         {
-          for (unsigned int j=0; j<this->n(); j++)
-           os << (*this)(i,j) << " ";
-          os << std::endl;
+         if(sparse)
+           {
+             for (unsigned int j=0; j<this->n(); j++)
+               {
+                 T c = (*this)(i,j);
+                 if (c != static_cast<T>(0.0))
+                   {
+                     os << i << " " << j << " " << c << std::endl;
+                   }
+               }
+           }
+         else
+           {
+             for (unsigned int j=0; j<this->n(); j++)
+               os << (*this)(i,j) << " ";
+             os << std::endl;
+           }
         }
 
       std::vector<unsigned int> ibuf, jbuf;
@@ -162,27 +176,46 @@
           unsigned int currentb = 0;
           for (;currenti <= ibuf.back(); ++currenti)
             {
-              for (unsigned int j=0; j<this->n(); j++)
-                {
-                  if (currentb < ibuf.size() &&
-                      ibuf[currentb] == currenti &&
-                      jbuf[currentb] == j)
-                    {
-                     os << cbuf[currentb] << " ";
-                     currentb++;
-                    }
-                  else
-                   os << static_cast<T>(0.0) << " ";
-                }
-              os << std::endl;
+             if(sparse)
+               {
+                 for (unsigned int j=0; j<this->n(); j++)
+                   {
+                     if (currentb < ibuf.size() &&
+                         ibuf[currentb] == currenti &&
+                         jbuf[currentb] == j)
+                       {
+                         os << currenti << " " << j << " " << cbuf[currentb] 
<< std::endl;
+                         currentb++;
+                       }
+                   }
+               }
+             else
+               {
+                 for (unsigned int j=0; j<this->n(); j++)
+                   {
+                     if (currentb < ibuf.size() &&
+                         ibuf[currentb] == currenti &&
+                         jbuf[currentb] == j)
+                       {
+                         os << cbuf[currentb] << " ";
+                         currentb++;
+                       }
+                     else
+                       os << static_cast<T>(0.0) << " ";
+                   }
+                 os << std::endl;
+               }
             }
         }
-      for (; currenti != this->m(); ++currenti)
-        {
-          for (unsigned int j=0; j<this->n(); j++)
-           os << static_cast<T>(0.0) << " ";
-          os << std::endl;
-        }
+      if(!sparse)
+       {
+         for (; currenti != this->m(); ++currenti)
+           {
+             for (unsigned int j=0; j<this->n(); j++)
+               os << static_cast<T>(0.0) << " ";
+             os << std::endl;
+           }
+       }
     }
   else
     {
------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Libmesh-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libmesh-devel

Reply via email to