← Back to team overview

dolfin team mailing list archive

[noreply@xxxxxxxxxxxxx: [Branch ~dolfin-core/dolfin/main] Rev 5497: First cut at cleaning up BlockMatrices.]

 

Great that the Block stuff is getting a cleanup/revisit.

--
Anders
--- Begin Message ---
------------------------------------------------------------
revno: 5497
committer: Garth N. Wells <gnw20@xxxxxxxxx>
branch nick: dolfin-all
timestamp: Thu 2011-01-13 20:04:55 +0000
message:
  First cut at cleaning up BlockMatrices.
modified:
  dolfin/la/BlockMatrix.cpp
  dolfin/la/BlockMatrix.h


--
lp:dolfin
https://code.launchpad.net/~dolfin-core/dolfin/main

Your team DOLFIN Core Team is subscribed to branch lp:dolfin.
To unsubscribe from this branch go to https://code.launchpad.net/~dolfin-core/dolfin/main/+edit-subscription
=== modified file 'dolfin/la/BlockMatrix.cpp'
--- dolfin/la/BlockMatrix.cpp	2011-01-13 19:42:13 +0000
+++ dolfin/la/BlockMatrix.cpp	2011-01-13 20:04:55 +0000
@@ -1,81 +1,78 @@
 // Copyright (C) 2008 Kent-Andre Mardal.
 // Licensed under the GNU LGPL Version 2.1.
 //
+// Modified by Anders Logg, 2008.
+// Modified by Garth N. Wells, 2011.
+//
 // First added:  2008-08-25
-// Last changed: 2009-08-11
-//
-// Modified by Anders Logg, 2008.
+// Last changed: 2011-01-13
 
 #include <iostream>
-#include <stdexcept>
+#include <boost/scoped_ptr.hpp>
 
 #include "dolfin/common/utils.h"
+#include "BlockVector.h"
 #include "DefaultFactory.h"
 #include "GenericVector.h"
-
-#include "BlockVector.h"
+#include "Matrix.h"
 #include "BlockMatrix.h"
 
 using namespace dolfin;
 
 //-----------------------------------------------------------------------------
-BlockMatrix::BlockMatrix(uint n_, uint m_, bool owner_)
-    : owner(owner_), n(n_), m(m_)
+BlockMatrix::BlockMatrix(uint m, uint n)
+    : matrices(m, std::vector<boost::shared_ptr<GenericMatrix> >(n))
 {
-  matrices = new Matrix*[n*m];
-  if (owner)
-  {
-    for (uint i = 0; i < n; i++)
-      for (uint j = 0; j<m; j++)
-        matrices[i*n + j] = new Matrix();
-  }
+  for (uint i = 0; i < m; i++)
+    for (uint j = 0; j < n; j++)
+      matrices[i][j].reset(new Matrix());
 }
 //-----------------------------------------------------------------------------
 BlockMatrix::~BlockMatrix()
 {
-  if (owner)
-  {
-    for (uint i = 0; i < n; i++)
-      for (uint j = 0; j<m; j++)
-        delete matrices[i*n + j];
-  }
-  delete [] matrices;
-}
-//-----------------------------------------------------------------------------
-void BlockMatrix::set(uint i, uint j, Matrix& m)
-{
-//  matrices[i*n+j] = m.copy(); //FIXME. not obvious that copy is the right thing
-  matrices[i*n+j] = &m;         //FIXME. not obvious that copy is the right thing
-}
-//-----------------------------------------------------------------------------
-const Matrix& BlockMatrix::get(uint i, uint j) const
-{
-  return *(matrices[i*n+j]);
-}
-//-----------------------------------------------------------------------------
-Matrix& BlockMatrix::get(uint i, uint j)
-{
-  return *(matrices[i*n+j]);
+  // Do nothing
+}
+//-----------------------------------------------------------------------------
+void BlockMatrix::set(uint i, uint j, GenericMatrix& m)
+{
+  error("BlockMatrix::set needs revision.");
+  //matrices[i*n+j] = m.copy(); //FIXME. not obvious that copy is the right thing
+  //matrices[i*n+j] = &m;         //FIXME. not obvious that copy is the right thing
+}
+//-----------------------------------------------------------------------------
+const GenericMatrix& BlockMatrix::get(uint i, uint j) const
+{
+  return *(matrices[i][j]);
+}
+//-----------------------------------------------------------------------------
+GenericMatrix& BlockMatrix::get(uint i, uint j)
+{
+  return *(matrices[i][j]);
 }
 //-----------------------------------------------------------------------------
 dolfin::uint BlockMatrix::size(uint dim) const
 {
-  if (dim == 0) return n;
-  if (dim == 1) return m;
-  error("BlockMatrix has rank 2!"); return 0;
+  if (dim == 0)
+    return matrices.size();
+  else if (dim == 1)
+    return matrices[0].size();
+  else
+    error("BlockMatrix has rank 2!");
+
+  return 0;
 }
 //-----------------------------------------------------------------------------
 void BlockMatrix::zero()
 {
-  for(uint i = 0; i < n; i++)
-    for(uint j = 0; j < n; j++)
-      this->get(i,j).zero();
+  for(uint i = 0; i < matrices.size(); i++)
+    for(uint j = 0; j < matrices[i].size(); j++)
+      this->get(i, j).zero();
 }
 //-----------------------------------------------------------------------------
 void BlockMatrix::apply(std::string mode)
 {
-  for(uint i = 0; i < n; i++)
-    for(uint j = 0; j < n; j++)
+  for(uint i = 0; i < matrices.size(); i++)
+    for(uint j = 0; j < matrices[i].size(); j++)
       this->get(i,j).apply(mode);
 }
 //-----------------------------------------------------------------------------
@@ -86,9 +83,9 @@
   if (verbose)
   {
     s << str(false) << std::endl << std::endl;
-    for (uint i = 0; i < n; i++)
+    for (uint i = 0; i < matrices.size(); i++)
     {
-      for (uint j = 0; i < m; j++)
+      for (uint j = 0; i < matrices[0].size(); j++)
       {
         s << "  BlockMatrix (" << i << ", " << j << ")" << std::endl << std::endl;
         s << indent(indent(get(i, j).str(true))) << std::endl;
@@ -96,7 +93,7 @@
     }
   }
   else
-    s << "<BlockMatrix containing " << n << " x " << m << " blocks>";
+    s << "<BlockMatrix containing " << matrices.size() << " x " << matrices[0].size() << " blocks>";
 
   return s.str();
 }
@@ -105,35 +102,32 @@
                        bool transposed) const
 {
   if (transposed)
-    error("BlockMatrix::mult: transposed not implemented");
+    error("BlockMatrix::mult: transposed not implemented.");
+
   DefaultFactory factory;
-  GenericVector* vec = factory.create_vector();
-  for(uint i = 0; i < n; i++)
+  boost::scoped_ptr<GenericVector> vec(factory.create_vector());
+  for(uint i = 0; i < matrices.size(); i++)
   {
     y.get(i).resize(this->get(i, 0).size(0));
     vec->resize(y.get(i).size());
+
     // FIXME: Do we need to zero the vector?
     y.get(i).zero();
     vec->zero();
-    for(uint j = 0; j < n; j++)
+    for(uint j = 0; j < matrices.size(); j++)
     {
       this->get(i, j).mult(x.get(j), *vec);
       y.get(i) += *vec;
     }
   }
-  delete vec;
 }
 //-----------------------------------------------------------------------------
 SubMatrix BlockMatrix::operator()(uint i, uint j)
 {
-  SubMatrix sm(i,j,*this);
+  SubMatrix sm(i, j, *this);
   return sm;
 }
 //-----------------------------------------------------------------------------
-//FIXME there are numerous functions that should be added
-
-//-----------------------------------------------------------------------------
-// SubMatrix
 //-----------------------------------------------------------------------------
 SubMatrix::SubMatrix(uint col, uint row, BlockMatrix& bm)
   : row(row), col(col), bm(bm)
@@ -146,16 +140,9 @@
   // Do nothing
 }
 //-----------------------------------------------------------------------------
-const SubMatrix& SubMatrix::operator=(Matrix& m)
+const SubMatrix& SubMatrix::operator=(GenericMatrix& m)
 {
   bm.set(row, col, m);
   return *this;
 }
-/*
-//-----------------------------------------------------------------------------
-Matrix& SubMatrix::operator()
-{
-  return bm.get(row, col);
-}
-*/
 //-----------------------------------------------------------------------------

=== modified file 'dolfin/la/BlockMatrix.h'
--- dolfin/la/BlockMatrix.h	2010-07-17 13:30:00 +0000
+++ dolfin/la/BlockMatrix.h	2011-01-13 20:04:55 +0000
@@ -1,30 +1,30 @@
 // Copyright (C) 2008 Kent-Andre Mardal.
 // Licensed under the GNU LGPL Version 2.1.
 //
+// Modified by Anders Logg, 2008.
+// Modified by Garth N. Wells, 2011.
+//
 // First added:  2008-08-25
-// Last changed: 2009-09-08
-//
-// Modified by Anders Logg, 2008.
+// Last changed: 2011-01-13
 
 #ifndef __BLOCKMATRIX_H
 #define __BLOCKMATRIX_H
 
-#include <map>
-#include "Matrix.h"
+#include <vector>
+#include <boost/shared_ptr.hpp>
 
 namespace dolfin
 {
-  // Forward declaration
+  // Forward declarations
+  class GenericMatrix;
   class SubMatrix;
 
   class BlockMatrix
   {
   public:
 
-    // FIXME: Change order of m and n
-
     // Constructor
-    BlockMatrix(uint n=0, uint m=0, bool owner=false);
+    BlockMatrix(uint m = 0, uint n = 0);
 
     // Destructor
     ~BlockMatrix();
@@ -33,13 +33,13 @@
     SubMatrix operator() (uint i, uint j);
 
     /// Set block
-    void set(uint i, uint j, Matrix& m);
+    void set(uint i, uint j, GenericMatrix& m);
 
     /// Get block (const version)
-    const Matrix& get(uint i, uint j) const;
+    const GenericMatrix& get(uint i, uint j) const;
 
     /// Get block
-    Matrix& get(uint i, uint j);
+    GenericMatrix& get(uint i, uint j);
 
     /// Return size of given dimension
     uint size(uint dim) const;
@@ -58,10 +58,7 @@
 
   private:
 
-    bool owner;
-    uint n, m;
-    //    std::map<std::pair<int,int>, Matrix*> matrices;
-    Matrix** matrices;
+    std::vector<std::vector<boost::shared_ptr<GenericMatrix> > > matrices;
 
   };
 
@@ -74,10 +71,11 @@
   public:
 
     SubMatrix(uint row, uint col, BlockMatrix& bm);
+
     ~SubMatrix();
 
     /// Assign Matrix to SubMatrix
-    const SubMatrix& operator= (Matrix& m);
+    const SubMatrix& operator= (GenericMatrix& m);
 
   private:
 


--- End Message ---